FileVoiceSender.php 2.0 KB

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