SmsVoiceVerifyCodeSender.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace addons\qcloudsms\library;
  3. use addons\qcloudsms\library\SmsSenderUtil;
  4. /**
  5. * 发送语音验证码类
  6. *
  7. */
  8. class SmsVoiceVerifyCodeSender
  9. {
  10. private $url;
  11. private $appid;
  12. private $appkey;
  13. private $util;
  14. /**
  15. * 构造函数
  16. *
  17. * @param string $appid sdkappid
  18. * @param string $appkey sdkappid对应的appkey
  19. */
  20. public function __construct($appid, $appkey)
  21. {
  22. $this->url = "https://yun.tim.qq.com/v5/tlsvoicesvr/sendvoice";
  23. $this->appid = $appid;
  24. $this->appkey = $appkey;
  25. $this->util = new SmsSenderUtil();
  26. }
  27. /**
  28. * 发送语音验证码
  29. *
  30. * @param string $nationCode 国家码,如 86 为中国
  31. * @param string $phoneNumber 不带国家码的手机号
  32. * @param string $msg 信息内容,必须与申请的模板格式一致,否则将返回错误
  33. * @param int $playtimes 播放次数,可选,最多3次,默认2次
  34. * @param string $ext 用户的session内容,服务端原样返回,可选字段,不需要可填空串
  35. * @return string 应答json字符串,详细内容参见腾讯云协议文档
  36. */
  37. public function send($nationCode, $phoneNumber, $msg, $playtimes = 2, $ext = "")
  38. {
  39. $random = $this->util->getRandom();
  40. $curTime = time();
  41. $wholeUrl = $this->url . "?sdkappid=" . $this->appid . "&random=" . $random;
  42. // 按照协议组织 post 包体
  43. $data = new \stdClass();
  44. $tel = new \stdClass();
  45. $tel->nationcode = "".$nationCode;
  46. $tel->mobile = "".$phoneNumber;
  47. $data->tel = $tel;
  48. $data->msg = $msg;
  49. $data->playtimes = $playtimes;
  50. // app凭证
  51. $data->sig = hash("sha256",
  52. "appkey=".$this->appkey."&random=".$random."&time="
  53. .$curTime."&mobile=".$phoneNumber, FALSE);
  54. // unix时间戳,请求发起时间,如果和系统时间相差超过10分钟则会返回失败
  55. $data->time = $curTime;
  56. $data->ext = $ext;
  57. return $this->util->sendCurlPost($wholeUrl, $data);
  58. }
  59. }