mirror of https://github.com/IoTcat/qcloudsms.git
parent
1a44e3b9f1
commit
e2aa1c2109
8 changed files with 767 additions and 2 deletions
@ -0,0 +1,3 @@ |
||||
/bin |
||||
.env |
||||
composer.lock |
@ -1,2 +1,169 @@ |
||||
# qcloudsms_php |
||||
qcloudsms sdk for php |
||||
腾讯云短信 PHP SDK |
||||
=== |
||||
|
||||
# Overview |
||||
|
||||
> 目前腾讯云短信为客户提供国内短信,国际短信,语音通知三大服务。 |
||||
|
||||
> 国内短信提供单发,群发,带模板ID单发,带模板ID群发以及短信回执与回复拉取。 |
||||
|
||||
> 国际短信可以直接使用国内单发接口,只需替换相应的国家码与手机号码。 |
||||
|
||||
> 语音通知目前支持语音验证码以及语音通知功能。 |
||||
|
||||
# Getting Start |
||||
|
||||
## 准备 |
||||
|
||||
- [ ] 申请APPID以及APPKey |
||||
|
||||
> 在开始本教程之前,您需要先获取APPID和APPkey,如您尚未申请,请到https://console.qcloud.com/sms/smslist 中添加应用,应用添加成功后您将获得APPID以及APPKey,注意APPID是以14xxxxx开头。 |
||||
|
||||
- [ ] 申请签名 |
||||
|
||||
> 下发短信必须携带签名,在相应服务模块 *短信内容配置* 中进行申请。 |
||||
|
||||
- [ ] 申请模板 |
||||
|
||||
> 下发短信内容必须经过审核,在相应服务 *短信内容配置* 中进行申请。 |
||||
|
||||
完成以上三项便可开始代码开发。 |
||||
|
||||
## 安装 |
||||
|
||||
qcloudsms_php采用composer进行安装,要使用qcloudsms功能,只需要在composer.json中添加如下依赖: |
||||
|
||||
```json |
||||
{ |
||||
"require": { |
||||
"qcloudsms/qcloudsms_php": "1.0.*" |
||||
} |
||||
} |
||||
``` |
||||
|
||||
## 用法 |
||||
|
||||
> 若您对接口存在疑问,可以查阅[API文档](http://static.javadoc.io/com.github.qcloudsms/sms/0.0.1/index.html?com/github/qcloudsms/package-summary.html)。 |
||||
|
||||
- **准备必要参数** |
||||
|
||||
```php |
||||
$appid = 122333333; |
||||
$appkey = "111111111112132312xx"; |
||||
$phoneNumber1 = "21212313123"; |
||||
$phoneNumber2 = "12345678902"; |
||||
$phoneNumber3 = "12345678903"; |
||||
$templId = 7839; |
||||
``` |
||||
|
||||
- **单发短信** |
||||
|
||||
```php |
||||
use Qcloud\Sms\SmsSingleSender; |
||||
|
||||
try { |
||||
$sender = new SmsSingleSender($appid, $appkey); |
||||
$result = $sender->send(0, "86", $phoneNumber2, |
||||
"测试短信,普通单发,深圳,小明,上学。", "", ""); |
||||
$rsp = json_decode($result); |
||||
echo $result; |
||||
} catch(\Exception $e) { |
||||
echo var_dump($e); |
||||
} |
||||
``` |
||||
|
||||
> `Note`: 如需发送国际短信,同样可以使用此接口,只需将国家码"86"改写成对应国家码号。 |
||||
|
||||
- **指定模板ID单发短信** |
||||
|
||||
```php |
||||
use Qcloud\Sms\SmsSingleSender; |
||||
|
||||
try { |
||||
$sender = new SmsSingleSender($appid, $appkey); |
||||
$params = ["指定模板单发", "深圳", "小明"]; |
||||
// 假设模板内容为:测试短信,{1},{2},{3},上学。 |
||||
$result = $sender->sendWithParam("86", $phoneNumber2, $templId, |
||||
$params, "", "", ""); |
||||
$rsp = json_decode($result); |
||||
echo $result; |
||||
} catch(\Exception $e) { |
||||
echo var_dump($e); |
||||
} |
||||
``` |
||||
|
||||
> `Note:`无论单发短信还是指定模板ID单发短信都需要从控制台中申请模板并且模板已经审核通过,才可能下发成功,否则返回失败。 |
||||
|
||||
- **群发** |
||||
|
||||
```php |
||||
use Qcloud\Sms\SmsMultiSender; |
||||
|
||||
try { |
||||
$phoneNumbers = [$phoneNumber1, $phoneNumber2, $phoneNumber3]; |
||||
$sender = new SmsMultiSender($appid, $appkey); |
||||
$result = $sender->send(0, "86", $phoneNumbers, |
||||
"测试短信,普通群发,深圳,小明,上学。", "", ""); |
||||
$rsp = json_decode($result); |
||||
echo $result; |
||||
} catch(\Exception $e) { |
||||
echo var_dump($e); |
||||
} |
||||
``` |
||||
|
||||
- **指定模板ID群发** |
||||
|
||||
```php |
||||
use Qcloud\Sms\SmsMultiSender; |
||||
|
||||
try { |
||||
$sender = new SmsMultiSender($appid, $appkey); |
||||
$params = ["指定模板群发", "深圳", "小明"]; |
||||
$result = $sender->sendWithParam("86", $phoneNumbers, |
||||
$templId, $params, "", "", ""); |
||||
$rsp = json_decode($result); |
||||
echo $result; |
||||
} catch(\Exception $e) { |
||||
echo var_dump($e); |
||||
} |
||||
``` |
||||
|
||||
> `Note:`群发一次请求最多支持200个号码,如有对号码数量有特殊需求请联系腾讯云短信技术支持(QQ:3012203387)。 |
||||
|
||||
- **发送语音验证码** |
||||
|
||||
```php |
||||
use Qcloud\Sms\SmsVoiceVerifyCodeSender; |
||||
|
||||
try { |
||||
$sender = new SmsVoiceVerifyCodeSender($appid, $appkey); |
||||
$result = $sender->send("86", $phoneNumber1, "1234", 2, ""); |
||||
$rsp = json_decode($result); |
||||
echo $result; |
||||
} catch (\Exception $e) { |
||||
echo var_dump($e); |
||||
} |
||||
``` |
||||
|
||||
> `Note`: 语音验证码发送只需提供验证码数字,例如在msg=“123”,您收到的语音通知为“您的语音验证码是1 2 3”,如需自定义内容,可以使用语音通知。 |
||||
|
||||
- **发送语音通知** |
||||
|
||||
```php |
||||
use Qcloud\Sms\SmsVoicePromptSender; |
||||
|
||||
try { |
||||
$sender = new SmsVoicePromptSender($appid, $appkey); |
||||
$result = $sender->send("86", $phoneNumber1, 2, "1234", ""); |
||||
$rsp = json_decode($result); |
||||
echo $result; |
||||
} catch (\Exception $e) { |
||||
echo var_dump($e); |
||||
} |
||||
``` |
||||
|
||||
> `Note:` 短信拉取功能需要联系腾讯云短信技术支持(QQ:3012203387),量大客户可以使用此功能批量拉取,其他客户不建议使用。 |
||||
|
||||
- **发送国际短信** |
||||
|
||||
国际短信参考单发短信 |
||||
|
@ -0,0 +1,12 @@ |
||||
{ |
||||
"name": "qcloudsms/qcloudsms_php", |
||||
"type": "library", |
||||
"description": "qcloud sms php sdk", |
||||
"keywords": ["qcloud", "sms", "php", "sdk"], |
||||
"license": "MIT", |
||||
"autoload": { |
||||
"psr-4": { |
||||
"Qcloud\\Sms\\": "src/" |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,185 @@ |
||||
<?php |
||||
|
||||
namespace Qcloud\Sms; |
||||
|
||||
use Qcloud\Sms\SmsSenderUtil; |
||||
|
||||
class SmsMultiSender |
||||
{ |
||||
private $url; |
||||
private $appid; |
||||
private $appkey; |
||||
private $util; |
||||
|
||||
public function __construct($appid, $appkey) |
||||
{ |
||||
$this->url = "https://yun.tim.qq.com/v5/tlssmssvr/sendmultisms2"; |
||||
$this->appid = $appid; |
||||
$this->appkey = $appkey; |
||||
$this->util = new SmsSenderUtil(); |
||||
} |
||||
|
||||
/** |
||||
* 普通群发 |
||||
* |
||||
* 普通群发需明确指定内容,如果有多个签名,请在内容中以【】的方式添加到信息内容中, |
||||
* 否则系统将使用默认签名。 |
||||
* |
||||
*【注意】海外短信无群发功能。 |
||||
* |
||||
* 请求包体: |
||||
* { |
||||
* "tel": [ |
||||
* { |
||||
* "nationcode": "86", |
||||
* "mobile": "13788888888" |
||||
* }, |
||||
* { |
||||
* "nationcode": "86", |
||||
* "mobile": "13788888889" |
||||
* } |
||||
* ], |
||||
* "type": 0, |
||||
* "msg": "你的验证码是1234", |
||||
* "sig": "fdba654e05bc0d15796713a1a1a2318c", |
||||
* "time": 1479888540, |
||||
* "extend": "", |
||||
* "ext": "" |
||||
* } |
||||
* |
||||
* 应答包体: |
||||
* { |
||||
* "result": 0, |
||||
* "errmsg": "OK", |
||||
* "ext": "", |
||||
* "detail": [ |
||||
* { |
||||
* "result": 0, |
||||
* "errmsg": "OK", |
||||
* "mobile": "13788888888", |
||||
* "nationcode": "86", |
||||
* "sid": "xxxxxxx", |
||||
* "fee": 1 |
||||
* }, |
||||
* { |
||||
* "result": 0, |
||||
* "errmsg": "OK", |
||||
* "mobile": "13788888889", |
||||
* "nationcode": "86", |
||||
* "sid": "xxxxxxx", |
||||
* "fee": 1 |
||||
* } |
||||
* ] |
||||
* } |
||||
* |
||||
* @param int $type 短信类型,0 为普通短信,1 营销短信 |
||||
* @param string $nationCode 国家码,如 86 为中国 |
||||
* @param string $phoneNumbers 不带国家码的手机号列表 |
||||
* @param string $msg 信息内容,必须与申请的模板格式一致,否则将返回错误 |
||||
* @param string $extend 扩展码,可填空串 |
||||
* @param string $ext 服务端原样返回的参数,可填空串 |
||||
* @return string json字符串,格式参考"应答包体",详细内容参见协议文档 |
||||
*/ |
||||
public function send($type, $nationCode, $phoneNumbers, $msg, $extend = "", $ext = "") |
||||
{ |
||||
$random = $this->util->getRandom(); |
||||
$curTime = time(); |
||||
$wholeUrl = $this->url . "?sdkappid=" . $this->appid . "&random=" . $random; |
||||
|
||||
$data = new \stdClass(); |
||||
$data->tel = $this->util->phoneNumbersToArray($nationCode, $phoneNumbers); |
||||
$data->type = $type; |
||||
$data->msg = $msg; |
||||
$data->sig = $this->util->calculateSig($this->appkey, $random, $curTime, $phoneNumbers); |
||||
$data->time = $curTime; |
||||
$data->extend = $extend; |
||||
$data->ext = $ext; |
||||
|
||||
return $this->util->sendCurlPost($wholeUrl, $data); |
||||
} |
||||
|
||||
/** |
||||
* 指定模板群发 |
||||
* |
||||
*【注意】海外短信无群发功能 |
||||
* |
||||
* 请求包体: |
||||
* { |
||||
* "tel": [ |
||||
* { |
||||
* "nationcode": "86", |
||||
* "mobile": "13788888888" |
||||
* }, |
||||
* { |
||||
* "nationcode": "86", |
||||
* "mobile": "13788888889" |
||||
* } |
||||
* ], |
||||
* "sign": "腾讯云", |
||||
* "tpl_id": 19, |
||||
* "params": [ |
||||
* "验证码", |
||||
* "1234", |
||||
* "4" |
||||
* ], |
||||
* "sig": "fdba654e05bc0d15796713a1a1a2318c", |
||||
* "time": 1479888540, |
||||
* "extend": "", |
||||
* "ext": "" |
||||
* } |
||||
* |
||||
* 应答包体: |
||||
* { |
||||
* "result": 0, |
||||
* "errmsg": "OK", |
||||
* "ext": "", |
||||
* "detail": [ |
||||
* { |
||||
* "result": 0, |
||||
* "errmsg": "OK", |
||||
* "mobile": "13788888888", |
||||
* "nationcode": "86", |
||||
* "sid": "xxxxxxx", |
||||
* "fee": 1 |
||||
* }, |
||||
* { |
||||
* "result": 0, |
||||
* "errmsg": "OK", |
||||
* "mobile": "13788888889", |
||||
* "nationcode": "86", |
||||
* "sid": "xxxxxxx", |
||||
* "fee": 1 |
||||
* } |
||||
* ] |
||||
* } |
||||
* |
||||
* @param string $nationCode 国家码,如 86 为中国 |
||||
* @param array $phoneNumbers 不带国家码的手机号列表 |
||||
* @param int $templId 模板id |
||||
* @param array $params 模板参数列表,如模板 {1}...{2}...{3},那么需要带三个参数 |
||||
* @param string $sign 签名,如果填空串,系统会使用默认签名 |
||||
* @param string $extend 扩展码,可填空串 |
||||
* @param string $ext 服务端原样返回的参数,可填空串 |
||||
* @return string json字符串,格式参考"应答包体",详细内容参见协议文档 |
||||
*/ |
||||
public function sendWithParam($nationCode, $phoneNumbers, $templId, $params, |
||||
$sign = "", $extend = "", $ext = "") |
||||
{ |
||||
$random = $this->util->getRandom(); |
||||
$curTime = time(); |
||||
$wholeUrl = $this->url . "?sdkappid=" . $this->appid . "&random=" . $random; |
||||
|
||||
$data = new \stdClass(); |
||||
$data->tel = $this->util->phoneNumbersToArray($nationCode, $phoneNumbers); |
||||
$data->sign = $sign; |
||||
$data->tpl_id = $templId; |
||||
$data->params = $params; |
||||
$data->sig = $this->util->calculateSigForTemplAndPhoneNumbers( |
||||
$this->appkey, $random, $curTime, $phoneNumbers); |
||||
$data->time = $curTime; |
||||
$data->extend = $extend; |
||||
$data->ext = $ext; |
||||
|
||||
return $this->util->sendCurlPost($wholeUrl, $data); |
||||
} |
||||
} |
@ -0,0 +1,86 @@ |
||||
<?php |
||||
|
||||
namespace Qcloud\Sms; |
||||
|
||||
class SmsSenderUtil |
||||
{ |
||||
public function getRandom() |
||||
{ |
||||
return rand(100000, 999999); |
||||
} |
||||
|
||||
public function calculateSig($appkey, $random, $curTime, $phoneNumbers) |
||||
{ |
||||
$phoneNumbersString = $phoneNumbers[0]; |
||||
for ($i = 1; $i < count($phoneNumbers); $i++) { |
||||
$phoneNumbersString .= ("," . $phoneNumbers[$i]); |
||||
} |
||||
|
||||
return hash("sha256", "appkey=".$appkey."&random=".$random |
||||
."&time=".$curTime."&mobile=".$phoneNumbersString); |
||||
} |
||||
|
||||
public function calculateSigForTemplAndPhoneNumbers($appkey, $random, |
||||
$curTime, $phoneNumbers) |
||||
{ |
||||
$phoneNumbersString = $phoneNumbers[0]; |
||||
for ($i = 1; $i < count($phoneNumbers); $i++) { |
||||
$phoneNumbersString .= ("," . $phoneNumbers[$i]); |
||||
} |
||||
|
||||
return hash("sha256", "appkey=".$appkey."&random=".$random |
||||
."&time=".$curTime."&mobile=".$phoneNumbersString); |
||||
} |
||||
|
||||
public function phoneNumbersToArray($nationCode, $phoneNumbers) |
||||
{ |
||||
$i = 0; |
||||
$tel = array(); |
||||
do { |
||||
$telElement = new \stdClass(); |
||||
$telElement->nationcode = $nationCode; |
||||
$telElement->mobile = $phoneNumbers[$i]; |
||||
array_push($tel, $telElement); |
||||
} while (++$i < count($phoneNumbers)); |
||||
|
||||
return $tel; |
||||
} |
||||
|
||||
public function calculateSigForTempl($appkey, $random, $curTime, $phoneNumber) |
||||
{ |
||||
$phoneNumbers = array($phoneNumber); |
||||
|
||||
return $this->calculateSigForTemplAndPhoneNumbers($appkey, $random, |
||||
$curTime, $phoneNumbers); |
||||
} |
||||
|
||||
public function sendCurlPost($url, $dataObj) |
||||
{ |
||||
$curl = curl_init(); |
||||
curl_setopt($curl, CURLOPT_URL, $url); |
||||
curl_setopt($curl, CURLOPT_HEADER, 0); |
||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
||||
curl_setopt($curl, CURLOPT_POST, 1); |
||||
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($dataObj)); |
||||
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); |
||||
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); |
||||
|
||||
$ret = curl_exec($curl); |
||||
if (false == $ret) { |
||||
// curl_exec failed |
||||
$result = "{ \"result\":" . -2 . ",\"errmsg\":\"" . curl_error($curl) . "\"}"; |
||||
} else { |
||||
$rsp = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
||||
if (200 != $rsp) { |
||||
$result = "{ \"result\":" . -1 . ",\"errmsg\":\"". $rsp |
||||
. " " . curl_error($curl) ."\"}"; |
||||
} else { |
||||
$result = $ret; |
||||
} |
||||
} |
||||
|
||||
curl_close($curl); |
||||
|
||||
return $result; |
||||
} |
||||
} |
@ -0,0 +1,148 @@ |
||||
<?php |
||||
|
||||
namespace Qcloud\Sms; |
||||
|
||||
use Qcloud\Sms\SmsSenderUtil; |
||||
|
||||
class SmsSingleSender |
||||
{ |
||||
private $url; |
||||
private $appid; |
||||
private $appkey; |
||||
private $util; |
||||
|
||||
public function __construct($appid, $appkey) |
||||
{ |
||||
$this->url = "https://yun.tim.qq.com/v5/tlssmssvr/sendsms"; |
||||
$this->appid = $appid; |
||||
$this->appkey = $appkey; |
||||
$this->util = new SmsSenderUtil(); |
||||
} |
||||
|
||||
/** |
||||
* 普通单发 |
||||
* |
||||
* 普通单发需明确指定内容,如果有多个签名,请在内容中以【】的方式添加到信息内容中,否则系统将使用默认签名。 |
||||
* |
||||
* 请求包体: |
||||
* { |
||||
* "tel": { |
||||
* "nationcode": "86", |
||||
* "mobile": "13788888888" |
||||
* }, |
||||
* "type": 0, |
||||
* "msg": "你的验证码是1234", |
||||
* "sig": "fdba654e05bc0d15796713a1a1a2318c", |
||||
* "time": 1479888540, |
||||
* "extend": "", |
||||
* "ext": "" |
||||
* } |
||||
* |
||||
* 应答包体: |
||||
* { |
||||
* "result": 0, |
||||
* "errmsg": "OK", |
||||
* "ext": "", |
||||
* "sid": "xxxxxxx", |
||||
* "fee": 1 |
||||
* } |
||||
* |
||||
* @param int $type 短信类型,0 为普通短信,1 营销短信 |
||||
* @param string $nationCode 国家码,如 86 为中国 |
||||
* @param string $phoneNumber 不带国家码的手机号 |
||||
* @param string $msg 信息内容,必须与申请的模板格式一致,否则将返回错误 |
||||
* @param string $extend 扩展码,可填空串 |
||||
* @param string $ext 服务端原样返回的参数,可填空串 |
||||
* @return string json字符串,格式参考"应答包体",详细内容参见协议文档 |
||||
*/ |
||||
public function send($type, $nationCode, $phoneNumber, $msg, $extend = "", $ext = "") |
||||
{ |
||||
$random = $this->util->getRandom(); |
||||
$curTime = time(); |
||||
$wholeUrl = $this->url . "?sdkappid=" . $this->appid . "&random=" . $random; |
||||
|
||||
// 按照协议组织 post 包体 |
||||
$data = new \stdClass(); |
||||
$tel = new \stdClass(); |
||||
$tel->nationcode = "".$nationCode; |
||||
$tel->mobile = "".$phoneNumber; |
||||
|
||||
$data->tel = $tel; |
||||
$data->type = (int)$type; |
||||
$data->msg = $msg; |
||||
$data->sig = hash("sha256", |
||||
"appkey=".$this->appkey."&random=".$random."&time=" |
||||
.$curTime."&mobile=".$phoneNumber, FALSE); |
||||
$data->time = $curTime; |
||||
$data->extend = $extend; |
||||
$data->ext = $ext; |
||||
|
||||
return $this->util->sendCurlPost($wholeUrl, $data); |
||||
} |
||||
|
||||
/** |
||||
* 指定模板单发 |
||||
* |
||||
* 请求包体: |
||||
* { |
||||
* "tel": { |
||||
* "nationcode": "86", |
||||
* "mobile": "13788888888" |
||||
* }, |
||||
* "sign": "腾讯云", |
||||
* "tpl_id": 19, |
||||
* "params": [ |
||||
* "验证码", |
||||
* "1234", |
||||
* "4" |
||||
* ], |
||||
* "sig": "fdba654e05bc0d15796713a1a1a2318c", |
||||
* "time": 1479888540, |
||||
* "extend": "", |
||||
* "ext": "" |
||||
* } |
||||
* |
||||
* 应答包体: |
||||
* { |
||||
* "result": 0, |
||||
* "errmsg": "OK", |
||||
* "ext": "", |
||||
* "sid": "xxxxxxx", |
||||
* "fee": 1 |
||||
* } |
||||
* |
||||
* @param string $nationCode 国家码,如 86 为中国 |
||||
* @param string $phoneNumber 不带国家码的手机号 |
||||
* @param int $templId 模板 id |
||||
* @param array $params 模板参数列表,如模板 {1}...{2}...{3},那么需要带三个参数 |
||||
* @param string $sign 签名,如果填空串,系统会使用默认签名 |
||||
* @param string $extend 扩展码,可填空串 |
||||
* @param string $ext 服务端原样返回的参数,可填空串 |
||||
* @return string json字符串,格式参考"应答包体",详细内容参见协议文档 |
||||
*/ |
||||
public function sendWithParam($nationCode, $phoneNumber, $templId = 0, $params, |
||||
$sign = "", $extend = "", $ext = "") |
||||
{ |
||||
$random = $this->util->getRandom(); |
||||
$curTime = time(); |
||||
$wholeUrl = $this->url . "?sdkappid=" . $this->appid . "&random=" . $random; |
||||
|
||||
// 按照协议组织 post 包体 |
||||
$data = new \stdClass(); |
||||
$tel = new \stdClass(); |
||||
$tel->nationcode = "".$nationCode; |
||||
$tel->mobile = "".$phoneNumber; |
||||
|
||||
$data->tel = $tel; |
||||
$data->sig = $this->util->calculateSigForTempl($this->appkey, $random, |
||||
$curTime, $phoneNumber); |
||||
$data->tpl_id = $templId; |
||||
$data->params = $params; |
||||
$data->sign = $sign; |
||||
$data->time = $curTime; |
||||
$data->extend = $extend; |
||||
$data->ext = $ext; |
||||
|
||||
return $this->util->sendCurlPost($wholeUrl, $data); |
||||
} |
||||
} |
@ -0,0 +1,85 @@ |
||||
<?php |
||||
|
||||
namespace Qcloud\Sms; |
||||
|
||||
use Qcloud\Sms\SmsSenderUtil; |
||||
|
||||
class SmsVoicePromptSender |
||||
{ |
||||
private $url; |
||||
private $appid; |
||||
private $appkey; |
||||
private $util; |
||||
|
||||
public function __construct($appid, $appkey) |
||||
{ |
||||
$this->url = "https://yun.tim.qq.com/v5/tlsvoicesvr/sendvoiceprompt"; |
||||
$this->appid = $appid; |
||||
$this->appkey = $appkey; |
||||
$this->util = new SmsSenderUtil(); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* 语言验证码发送 |
||||
* |
||||
* 请求包体: |
||||
* { |
||||
* "tel": { |
||||
* "nationcode": "86", |
||||
* "mobile": "13788888888" |
||||
* }, |
||||
* "prompttype": 2, |
||||
* "promptfile": "语音内容文本", |
||||
* "playtimes": 2, |
||||
* "sig": "30db206bfd3fea7ef0db929998642c8ea54cc7042a779c5a0d9897358f6e9505", |
||||
* "time": 1457336869, |
||||
* "ext": "" |
||||
* } |
||||
* |
||||
* 应答包体: |
||||
* { |
||||
* "result": 0, |
||||
* "errmsg": "OK", |
||||
* "ext": "", |
||||
* "sid": "xxxxxxx", |
||||
* "fee": 1 |
||||
* } |
||||
* |
||||
* @param string $nationCode 国家码,如 86 为中国 |
||||
* @param string $phoneNumber 不带国家码的手机号 |
||||
* @param string $prompttype 语音类型,目前固定为2 |
||||
* @param string $msg 信息内容,必须与申请的模板格式一致,否则将返回错误 |
||||
* @param string $playtimes 播放次数,可选,最多3次,默认2次 |
||||
* @param string $ext 用户的session内容,服务端原样返回,可选字段,不需要可填空串 |
||||
* @return string json字符串,格式参考"应答包体",详细内容参见协议文档 |
||||
*/ |
||||
public function send($nationCode, $phoneNumber, $prompttype, $msg, $playtimes = 2, $ext = "") |
||||
{ |
||||
$random = $this->util->getRandom(); |
||||
$curTime = time(); |
||||
$wholeUrl = $this->url . "?sdkappid=" . $this->appid . "&random=" . $random; |
||||
|
||||
// 按照协议组织 post 包体 |
||||
$data = new \stdClass(); |
||||
$tel = new \stdClass(); |
||||
$tel->nationcode = "".$nationCode; |
||||
$tel->mobile = "".$phoneNumber; |
||||
|
||||
$data->tel = $tel; |
||||
// 通知内容,utf8编码,支持中文英文、数字及组合,需要和语音内容模版相匹配 |
||||
$data->promptfile = $msg; |
||||
// 固定值 2 |
||||
$data->prompttype = $prompttype; |
||||
$data->playtimes = $playtimes; |
||||
// app凭证 |
||||
$data->sig = hash("sha256", |
||||
"appkey=".$this->appkey."&random=".$random."&time=" |
||||
.$curTime."&mobile=".$phoneNumber, FALSE); |
||||
// unix时间戳,请求发起时间,如果和系统时间相差超过10分钟则会返回失败 |
||||
$data->time = $curTime; |
||||
$data->ext = $ext; |
||||
|
||||
return $this->util->sendCurlPost($wholeUrl, $data); |
||||
} |
||||
} |
@ -0,0 +1,79 @@ |
||||
<?php |
||||
|
||||
namespace Qcloud\Sms; |
||||
|
||||
use Qcloud\Sms\SmsSenderUtil; |
||||
|
||||
class SmsVoiceVerifyCodeSender |
||||
{ |
||||
private $url; |
||||
private $appid; |
||||
private $appkey; |
||||
private $util; |
||||
|
||||
public function __construct($appid, $appkey) |
||||
{ |
||||
$this->url = "https://yun.tim.qq.com/v5/tlsvoicesvr/sendvoice"; |
||||
$this->appid = $appid; |
||||
$this->appkey = $appkey; |
||||
$this->util = new SmsSenderUtil(); |
||||
} |
||||
|
||||
/** |
||||
* 语言验证码发送 |
||||
* |
||||
* 请求包体: |
||||
* { |
||||
* "tel": { |
||||
* "nationcode": "86", |
||||
* "mobile": "13788888888" |
||||
* }, |
||||
* "msg": "1234", |
||||
* "playtimes": 2, |
||||
* "sig": "30db206bfd3fea7ef0db929998642c8ea54cc7042a779c5a0d9897358f6e9505", |
||||
* "time": 1457336869, |
||||
* "ext": "" |
||||
* } |
||||
* |
||||
* 应答包体: |
||||
* { |
||||
* "result": 0, |
||||
* "errmsg": "OK", |
||||
* "ext": "", |
||||
* "sid": "xxxxxxx", |
||||
* "fee": 1 |
||||
* } |
||||
* |
||||
* @param string $nationCode 国家码,如 86 为中国 |
||||
* @param string $phoneNumber 不带国家码的手机号 |
||||
* @param string $msg 信息内容,必须与申请的模板格式一致,否则将返回错误 |
||||
* @param int $playtimes 播放次数,可选,最多3次,默认2次 |
||||
* @param string $ext 用户的session内容,服务端原样返回,可选字段,不需要可填空串 |
||||
* @return string json字符串,格式参考"应答包体",详细内容参见协议文档 |
||||
*/ |
||||
public function send($nationCode, $phoneNumber, $msg, $playtimes = 2, $ext = "") |
||||
{ |
||||
$random = $this->util->getRandom(); |
||||
$curTime = time(); |
||||
$wholeUrl = $this->url . "?sdkappid=" . $this->appid . "&random=" . $random; |
||||
|
||||
// 按照协议组织 post 包体 |
||||
$data = new \stdClass(); |
||||
$tel = new \stdClass(); |
||||
$tel->nationcode = "".$nationCode; |
||||
$tel->mobile = "".$phoneNumber; |
||||
|
||||
$data->tel = $tel; |
||||
$data->msg = $msg; |
||||
$data->playtimes = $playtimes; |
||||
// app凭证 |
||||
$data->sig = hash("sha256", |
||||
"appkey=".$this->appkey."&random=".$random."&time=" |
||||
.$curTime."&mobile=".$phoneNumber, FALSE); |
||||
// unix时间戳,请求发起时间,如果和系统时间相差超过10分钟则会返回失败 |
||||
$data->time = $curTime; |
||||
$data->ext = $ext; |
||||
|
||||
return $this->util->sendCurlPost($wholeUrl, $data); |
||||
} |
||||
} |
Loading…
Reference in new issue