SmsMultiSender.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace addons\qcloudsms\library;
  3. use addons\qcloudsms\library\SmsSenderUtil;
  4. /**
  5. * 群发短信类
  6. *
  7. */
  8. class SmsMultiSender
  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/tlssmssvr/sendmultisms2";
  23. $this->appid = $appid;
  24. $this->appkey = $appkey;
  25. $this->util = new SmsSenderUtil();
  26. }
  27. /**
  28. * 普通群发
  29. *
  30. * 普通群发需明确指定内容,如果有多个签名,请在内容中以【】的方式添加到信息内容中,
  31. * 否则系统将使用默认签名。
  32. *
  33. *
  34. * @param int $type 短信类型,0 为普通短信,1 营销短信
  35. * @param string $nationCode 国家码,如 86 为中国
  36. * @param array $phoneNumbers 不带国家码的手机号列表
  37. * @param string $msg 信息内容,必须与申请的模板格式一致,否则将返回错误
  38. * @param string $extend 扩展码,可填空串
  39. * @param string $ext 服务端原样返回的参数,可填空串
  40. * @return string 应答json字符串,详细内容参见腾讯云协议文档
  41. */
  42. public function send($type, $nationCode, $phoneNumbers, $msg, $extend = "", $ext = "")
  43. {
  44. $random = $this->util->getRandom();
  45. $curTime = time();
  46. $wholeUrl = $this->url . "?sdkappid=" . $this->appid . "&random=" . $random;
  47. $data = new \stdClass();
  48. $data->tel = $this->util->phoneNumbersToArray($nationCode, $phoneNumbers);
  49. $data->type = $type;
  50. $data->msg = $msg;
  51. $data->sig = $this->util->calculateSig($this->appkey, $random,
  52. $curTime, $phoneNumbers);
  53. $data->time = $curTime;
  54. $data->extend = $extend;
  55. $data->ext = $ext;
  56. return $this->util->sendCurlPost($wholeUrl, $data);
  57. }
  58. /**
  59. * 指定模板群发
  60. *
  61. *
  62. * @param string $nationCode 国家码,如 86 为中国
  63. * @param array $phoneNumbers 不带国家码的手机号列表
  64. * @param int $templId 模板id
  65. * @param array $params 模板参数列表,如模板 {1}...{2}...{3},那么需要带三个参数
  66. * @param string $sign 签名,如果填空串,系统会使用默认签名
  67. * @param string $extend 扩展码,可填空串
  68. * @param string $ext 服务端原样返回的参数,可填空串
  69. * @return string 应答json字符串,详细内容参见腾讯云协议文档
  70. */
  71. public function sendWithParam($nationCode, $phoneNumbers, $templId, $params,
  72. $sign = "", $extend = "", $ext = "")
  73. {
  74. $random = $this->util->getRandom();
  75. $curTime = time();
  76. $wholeUrl = $this->url . "?sdkappid=" . $this->appid . "&random=" . $random;
  77. $data = new \stdClass();
  78. $data->tel = $this->util->phoneNumbersToArray($nationCode, $phoneNumbers);
  79. $data->sign = $sign;
  80. $data->tpl_id = $templId;
  81. $data->params = $params;
  82. $data->sig = $this->util->calculateSigForTemplAndPhoneNumbers(
  83. $this->appkey, $random, $curTime, $phoneNumbers);
  84. $data->time = $curTime;
  85. $data->extend = $extend;
  86. $data->ext = $ext;
  87. return $this->util->sendCurlPost($wholeUrl, $data);
  88. }
  89. }