Qcloud.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace app\common\extend\sms;
  3. class Qcloud {
  4. public $name = '腾讯云短信';
  5. public $ver = '2.0';
  6. private $url;
  7. private $appid;
  8. private $appkey;
  9. public function __construct()
  10. {
  11. $this->url = "https://yun.tim.qq.com/v5/tlssmssvr/sendsms";
  12. $this->appid = $GLOBALS['config']['sms']['qcloud']['appid'];
  13. $this->appkey = $GLOBALS['config']['sms']['qcloud']['appkey'];
  14. }
  15. /**
  16. * 生成随机数
  17. *
  18. * @return int 随机数结果
  19. */
  20. public function getRandom()
  21. {
  22. return rand(100000, 999999);
  23. }
  24. /**
  25. * 生成签名
  26. *
  27. * @param string $appkey sdkappid对应的appkey
  28. * @param string $random 随机正整数
  29. * @param string $curTime 当前时间
  30. * @param array $phoneNumbers 手机号码
  31. * @return string 签名结果
  32. */
  33. public function calculateSig($appkey, $random, $curTime, $phoneNumbers)
  34. {
  35. $phoneNumbersString = $phoneNumbers[0];
  36. for ($i = 1; $i < count($phoneNumbers); $i++) {
  37. $phoneNumbersString .= ("," . $phoneNumbers[$i]);
  38. }
  39. return hash("sha256", "appkey=".$appkey."&random=".$random
  40. ."&time=".$curTime."&mobile=".$phoneNumbersString);
  41. }
  42. /**
  43. * 生成签名
  44. *
  45. * @param string $appkey sdkappid对应的appkey
  46. * @param string $random 随机正整数
  47. * @param string $curTime 当前时间
  48. * @param array $phoneNumbers 手机号码
  49. * @return string 签名结果
  50. */
  51. public function calculateSigForTemplAndPhoneNumbers($appkey, $random,
  52. $curTime, $phoneNumbers)
  53. {
  54. $phoneNumbersString = $phoneNumbers[0];
  55. for ($i = 1; $i < count($phoneNumbers); $i++) {
  56. $phoneNumbersString .= ("," . $phoneNumbers[$i]);
  57. }
  58. return hash("sha256", "appkey=".$appkey."&random=".$random
  59. ."&time=".$curTime."&mobile=".$phoneNumbersString);
  60. }
  61. public function phoneNumbersToArray($nationCode, $phoneNumbers)
  62. {
  63. $i = 0;
  64. $tel = array();
  65. do {
  66. $telElement = new \stdClass();
  67. $telElement->nationcode = $nationCode;
  68. $telElement->mobile = $phoneNumbers[$i];
  69. array_push($tel, $telElement);
  70. } while (++$i < count($phoneNumbers));
  71. return $tel;
  72. }
  73. /**
  74. * 生成签名
  75. *
  76. * @param string $appkey sdkappid对应的appkey
  77. * @param string $random 随机正整数
  78. * @param string $curTime 当前时间
  79. * @param array $phoneNumber 手机号码
  80. * @return string 签名结果
  81. */
  82. public function calculateSigForTempl($appkey, $random, $curTime, $phoneNumber)
  83. {
  84. $phoneNumbers = array($phoneNumber);
  85. return $this->calculateSigForTemplAndPhoneNumbers($appkey, $random,
  86. $curTime, $phoneNumbers);
  87. }
  88. /**
  89. * 生成签名
  90. *
  91. * @param string $appkey sdkappid对应的appkey
  92. * @param string $random 随机正整数
  93. * @param string $curTime 当前时间
  94. * @return string 签名结果
  95. */
  96. public function calculateSigForPuller($appkey, $random, $curTime)
  97. {
  98. return hash("sha256", "appkey=".$appkey."&random=".$random
  99. ."&time=".$curTime);
  100. }
  101. /**
  102. * 生成上传文件授权
  103. *
  104. * @param string $appkey sdkappid对应的appkey
  105. * @param string $random 随机正整数
  106. * @param string $curTime 当前时间
  107. * @param array $fileSha1Sum 文件sha1sum
  108. * @return string 授权结果
  109. */
  110. public function calculateAuth($appkey, $random, $curTime, $fileSha1Sum)
  111. {
  112. return hash("sha256", "appkey=".$appkey."&random=".$random
  113. ."&time=".$curTime."&content-sha1=".$fileSha1Sum);
  114. }
  115. /**
  116. * 生成sha1sum
  117. *
  118. * @param string $content 内容
  119. * @return string 内容sha1散列值
  120. */
  121. public function sha1sum($content)
  122. {
  123. return hash("sha1", $content);
  124. }
  125. /**
  126. * 发送请求
  127. *
  128. * @param string $url 请求地址
  129. * @param array $dataObj 请求内容
  130. * @return string 应答json字符串
  131. */
  132. public function sendCurlPost($url, $dataObj)
  133. {
  134. $curl = curl_init();
  135. curl_setopt($curl, CURLOPT_URL, $url);
  136. curl_setopt($curl, CURLOPT_HEADER, 0);
  137. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  138. curl_setopt($curl, CURLOPT_POST, 1);
  139. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60);
  140. curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($dataObj));
  141. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
  142. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  143. $ret = curl_exec($curl);
  144. if (false == $ret) {
  145. // curl_exec failed
  146. $result = "{ \"result\":" . -2 . ",\"errmsg\":\"" . curl_error($curl) . "\"}";
  147. } else {
  148. $rsp = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  149. if (200 != $rsp) {
  150. $result = "{ \"result\":" . -1 . ",\"errmsg\":\"". $rsp
  151. . " " . curl_error($curl) ."\"}";
  152. } else {
  153. $result = $ret;
  154. }
  155. }
  156. curl_close($curl);
  157. return $result;
  158. }
  159. public function sendWithParam($nationCode, $phoneNumber, $templId = 0, $params,
  160. $sign = "", $extend = "", $ext = "")
  161. {
  162. $random = $this->getRandom();
  163. $curTime = time();
  164. $wholeUrl = $this->url . "?sdkappid=" . $this->appid . "&random=" . $random;
  165. // 按照协议组织 post 包体
  166. $data = new \stdClass();
  167. $tel = new \stdClass();
  168. $tel->nationcode = "".$nationCode;
  169. $tel->mobile = "".$phoneNumber;
  170. $data->tel = $tel;
  171. $data->sig = $this->calculateSigForTempl($this->appkey, $random,
  172. $curTime, $phoneNumber);
  173. $data->tpl_id = $templId;
  174. $data->params = $params;
  175. $data->sign = $sign;
  176. $data->time = $curTime;
  177. $data->extend = $extend;
  178. $data->ext = $ext;
  179. return $this->sendCurlPost($wholeUrl, $data);
  180. }
  181. public function submit($phone,$code,$type_flag,$type_des,$text)
  182. {
  183. if(empty($phone) || empty($code) || empty($type_flag)){
  184. return ['code'=>101,'msg'=>'参数错误'];
  185. }
  186. $sign = $GLOBALS['config']['sms']['sign'];
  187. $tpl = $GLOBALS['config']['sms']['tpl_code_'.$type_flag];
  188. $params = [
  189. $code
  190. ];
  191. try {
  192. $result = $this->sendWithParam("86", $phone, $tpl, $params, $sign, "", "");
  193. $rsp = json_decode($result,true);
  194. if($rsp['ErrorCode'] !==''){
  195. return ['code'=>1,'msg'=>'ok'];
  196. }
  197. return ['code'=>101,'msg'=>$rsp['errmsg']];
  198. }
  199. catch(\Exception $e) {
  200. return ['code'=>102,'msg'=>'发生异常请重试'];
  201. }
  202. }
  203. }