SmsSingleSender.php 3.6 KB

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