TtsVoiceSender.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace addons\qcloudsms\library;
  3. use addons\qcloudsms\library\SmsSenderUtil;
  4. /**
  5. * 指定模板发送语音通知类
  6. *
  7. */
  8. class TtsVoiceSender
  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://cloud.tim.qq.com/v5/tlsvoicesvr/sendtvoice";
  23. $this->appid = $appid;
  24. $this->appkey = $appkey;
  25. $this->util = new SmsSenderUtil();
  26. }
  27. /**
  28. *
  29. * 指定模板发送语音短信
  30. *
  31. * @param string $nationCode 国家码,如 86 为中国
  32. * @param string $phoneNumber 不带国家码的手机号
  33. * @param int $templId 模板 id
  34. * @param array $params 模板参数列表,如模板 {1}...{2}...{3},需要带三个参数
  35. * @param string $playtimes 播放次数,可选,最多3次,默认2次
  36. * @param string $ext 用户的session内容,服务端原样返回,可选字段,不需要可填空串
  37. * @return string 应答json字符串,详细内容参见腾讯云协议文档
  38. */
  39. public function send($nationCode, $phoneNumber, $templId, $params, $playtimes = 2, $ext = "")
  40. {
  41. /*var_dump($nationCode);
  42. var_dump($phoneNumber);
  43. var_dump($templId);
  44. var_dump($params);
  45. var_dump($playtimes);
  46. exit();*/
  47. $random = $this->util->getRandom();
  48. $curTime = time();
  49. $wholeUrl = $this->url . "?sdkappid=" . $this->appid . "&random=" . $random;
  50. // 按照协议组织 post 包体
  51. $data = new \stdClass();
  52. $tel = new \stdClass();
  53. $tel->nationcode = "".$nationCode;
  54. $tel->mobile = "".$phoneNumber;
  55. $data->tel = $tel;
  56. $data->tpl_id = $templId;
  57. $data->params = $params;
  58. $data->playtimes = $playtimes;
  59. // app凭证
  60. $data->sig = $this->util->calculateSig($this->appkey, $random,
  61. $curTime, array($phoneNumber));
  62. // unix时间戳,请求发起时间,如果和系统时间相差超过10分钟则会返回失败
  63. $data->time = $curTime;
  64. $data->ext = $ext;
  65. //var_dump($data);exit();
  66. return $this->util->sendCurlPost($wholeUrl, $data);
  67. }
  68. }