85 lines
3.6 KiB
PHP
85 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace ThirdParty\SmsEmms;
|
|
|
|
use GuzzleHttp\Client;
|
|
use GuzzleHttp\Exception\RequestException;
|
|
|
|
class Sms
|
|
{
|
|
private $ci;
|
|
|
|
private $apiUrl = 'https://sdk.53api.com/api/webservice';
|
|
private $eprId = 3537;
|
|
private $userId = 'szcmckj';
|
|
private $password = 'Szcmckj3537';
|
|
private $logDir = 'sendSms';
|
|
|
|
const CMD = 'send';
|
|
|
|
public function __construct()
|
|
{
|
|
$this->ci = &get_instance();
|
|
$this->ci->load->library('myResponse');
|
|
}
|
|
|
|
/**
|
|
* 发送短信
|
|
* @param $mobile
|
|
* @param $content
|
|
* @return \MyResponse
|
|
*/
|
|
public function send($mobile, $content)
|
|
{
|
|
try {
|
|
$params = [
|
|
'cmd' => self::CMD, //String 是 命令字 send
|
|
'eprId' => $this->eprId, // int 是 企业ID
|
|
'userId' => $this->userId, // String 是 账号
|
|
'timestamp' => ceil(microtime(true) * 1000), // String 是 时间戳(1970.1.1至今的总秒毫数),长度13
|
|
'format' => 1, // int 是 返回结果格式,1: json方式返回 2:xml方式返回 1
|
|
// 'msgid' => '', // int 否 消息编号,客户端生成,唯一性;每提交一次msgid要不同;长度<=32
|
|
'mobile' => $mobile, // String 是 手机号码,多个号码用应为逗号隔开最多500个号码;
|
|
'content' => $content, // String 是 发送内容 需要urlencoder,utf-8编码 内容无需加签名,由系统自动添加;
|
|
// 'extno' => '', // int 否 扩展号,限制4位以内(包含4位),必须纯数字
|
|
// 'senddate' => '', // String 否 定时时间,不传则立即发送,格式yyyy-MM-dd HH:mm:ss
|
|
];
|
|
//Md5(企业Id+用户Id+用户密码+时间戳),”+”是连接符,不做为字符写入。MD5 32位小写
|
|
$params['key'] = md5($params['eprId'] . $params['userId'] . $this->password . $params['timestamp']);
|
|
list($req, $httpStatus) = $this->httpRequestPost($this->apiUrl, $params);
|
|
if ($httpStatus != 200) {
|
|
throw new \Exception('短信请求接口失败');
|
|
}
|
|
$resultData = json_decode($req, true);
|
|
if ($resultData['result'] != 1) {
|
|
throw new \Exception($resultData['tips']);
|
|
}
|
|
return new \MyResponse(EXIT_SUCCESS, '短信已发送');
|
|
} catch (\Exception $e) {
|
|
return new \MyResponse($e->getCode(), $e->getMessage());
|
|
}
|
|
}
|
|
|
|
private function httpRequestPost($url, $formData = [])
|
|
{
|
|
$logPath = "sendSms.log";
|
|
try {
|
|
$client = new Client();
|
|
debug_log("请求地址: {$url}", $logPath, $this->logDir);
|
|
debug_log("请求参数:" . json_encode($formData, JSON_UNESCAPED_UNICODE), $logPath, $this->logDir);
|
|
$response = $client->post($url, [
|
|
'headers' => [
|
|
'Content-Type' => 'application/x-www-form-urlencoded',
|
|
'Accept' => 'application/json'
|
|
],
|
|
'form_params' => $formData
|
|
]);
|
|
$responseContent = $response->getBody()->getContents();
|
|
debug_log("返回结果:" . $responseContent, $logPath, $this->logDir);
|
|
return [$responseContent, $response->getStatusCode()];
|
|
} catch (RequestException $e) {
|
|
debug_log("", $logPath, $this->logDir);
|
|
return [$e->getMessage(), 0];
|
|
}
|
|
}
|
|
} |