SmsSenderUtil.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace addons\qcloudsms\library;
  3. /**
  4. * 发送Util类
  5. *
  6. */
  7. class SmsSenderUtil
  8. {
  9. /**
  10. * 生成随机数
  11. *
  12. * @return int 随机数结果
  13. */
  14. public function getRandom()
  15. {
  16. return rand(100000, 999999);
  17. }
  18. /**
  19. * 生成签名
  20. *
  21. * @param string $appkey sdkappid对应的appkey
  22. * @param string $random 随机正整数
  23. * @param string $curTime 当前时间
  24. * @param array $phoneNumbers 手机号码
  25. * @return string 签名结果
  26. */
  27. public function calculateSig($appkey, $random, $curTime, $phoneNumbers)
  28. {
  29. $phoneNumbersString = $phoneNumbers[0];
  30. for ($i = 1; $i < count($phoneNumbers); $i++) {
  31. $phoneNumbersString .= ("," . $phoneNumbers[$i]);
  32. }
  33. return hash("sha256", "appkey=".$appkey."&random=".$random
  34. ."&time=".$curTime."&mobile=".$phoneNumbersString);
  35. }
  36. /**
  37. * 生成签名
  38. *
  39. * @param string $appkey sdkappid对应的appkey
  40. * @param string $random 随机正整数
  41. * @param string $curTime 当前时间
  42. * @param array $phoneNumbers 手机号码
  43. * @return string 签名结果
  44. */
  45. public function calculateSigForTemplAndPhoneNumbers($appkey, $random,
  46. $curTime, $phoneNumbers)
  47. {
  48. $phoneNumbersString = $phoneNumbers[0];
  49. for ($i = 1; $i < count($phoneNumbers); $i++) {
  50. $phoneNumbersString .= ("," . $phoneNumbers[$i]);
  51. }
  52. return hash("sha256", "appkey=".$appkey."&random=".$random
  53. ."&time=".$curTime."&mobile=".$phoneNumbersString);
  54. }
  55. public function phoneNumbersToArray($nationCode, $phoneNumbers)
  56. {
  57. $i = 0;
  58. $tel = array();
  59. do {
  60. $telElement = new \stdClass();
  61. $telElement->nationcode = $nationCode;
  62. $telElement->mobile = $phoneNumbers[$i];
  63. array_push($tel, $telElement);
  64. } while (++$i < count($phoneNumbers));
  65. return $tel;
  66. }
  67. /**
  68. * 生成签名
  69. *
  70. * @param string $appkey sdkappid对应的appkey
  71. * @param string $random 随机正整数
  72. * @param string $curTime 当前时间
  73. * @param array $phoneNumber 手机号码
  74. * @return string 签名结果
  75. */
  76. public function calculateSigForTempl($appkey, $random, $curTime, $phoneNumber)
  77. {
  78. $phoneNumbers = array($phoneNumber);
  79. return $this->calculateSigForTemplAndPhoneNumbers($appkey, $random,
  80. $curTime, $phoneNumbers);
  81. }
  82. /**
  83. * 生成签名
  84. *
  85. * @param string $appkey sdkappid对应的appkey
  86. * @param string $random 随机正整数
  87. * @param string $curTime 当前时间
  88. * @return string 签名结果
  89. */
  90. public function calculateSigForPuller($appkey, $random, $curTime)
  91. {
  92. return hash("sha256", "appkey=".$appkey."&random=".$random
  93. ."&time=".$curTime);
  94. }
  95. /**
  96. * 生成上传文件授权
  97. *
  98. * @param string $appkey sdkappid对应的appkey
  99. * @param string $random 随机正整数
  100. * @param string $curTime 当前时间
  101. * @param array $fileSha1Sum 文件sha1sum
  102. * @return string 授权结果
  103. */
  104. public function calculateAuth($appkey, $random, $curTime, $fileSha1Sum)
  105. {
  106. return hash("sha256", "appkey=".$appkey."&random=".$random
  107. ."&time=".$curTime."&content-sha1=".$fileSha1Sum);
  108. }
  109. /**
  110. * 生成sha1sum
  111. *
  112. * @param string $content 内容
  113. * @return string 内容sha1散列值
  114. */
  115. public function sha1sum($content)
  116. {
  117. return hash("sha1", $content);
  118. }
  119. /**
  120. * 发送请求
  121. *
  122. * @param string $url 请求地址
  123. * @param array $dataObj 请求内容
  124. * @return string 应答json字符串
  125. */
  126. public function sendCurlPost($url, $dataObj)
  127. {
  128. $curl = curl_init();
  129. curl_setopt($curl, CURLOPT_URL, $url);
  130. curl_setopt($curl, CURLOPT_HEADER, 0);
  131. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  132. curl_setopt($curl, CURLOPT_POST, 1);
  133. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60);
  134. curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($dataObj));
  135. curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  136. 'Content-Type: application/json; charset=utf-8',
  137. 'Content-Length: ' . strlen(json_encode($dataObj)))
  138. );
  139. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
  140. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  141. $ret = curl_exec($curl);
  142. if (false == $ret) {
  143. // curl_exec failed
  144. $result = "{ \"result\":" . -2 . ",\"errmsg\":\"" . curl_error($curl) . "\"}";
  145. } else {
  146. $rsp = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  147. if (200 != $rsp) {
  148. $result = "{ \"result\":" . -1 . ",\"errmsg\":\"". $rsp
  149. . " " . curl_error($curl) ."\"}";
  150. } else {
  151. $result = $ret;
  152. }
  153. }
  154. curl_close($curl);
  155. return $result;
  156. }
  157. /**
  158. * 发送请求
  159. *
  160. * @param string $req 请求对象
  161. * @return string 应答json字符串
  162. */
  163. public function fetch($req)
  164. {
  165. $curl = curl_init();
  166. curl_setopt($curl, CURLOPT_URL, $req->url);
  167. curl_setopt($curl, CURLOPT_HTTPHEADER, $req->headers);
  168. curl_setopt($curl, CURLOPT_POSTFIELDS, $req->body);
  169. curl_setopt($curl, CURLOPT_HEADER, 0);
  170. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  171. curl_setopt($curl, CURLOPT_POST, 1);
  172. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60);
  173. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
  174. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  175. $result = curl_exec($curl);
  176. if (false == $result) {
  177. // curl_exec failed
  178. $result = "{ \"result\":" . -2 . ",\"errmsg\":\"" . curl_error($curl) . "\"}";
  179. } else {
  180. $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  181. if (200 != $code) {
  182. $result = "{ \"result\":" . -1 . ",\"errmsg\":\"". $rsp
  183. . " " . curl_error($curl) ."\"}";
  184. }
  185. }
  186. curl_close($curl);
  187. return $result;
  188. }
  189. }