Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

SmsSenderUtil.php 6.0KB

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