Sms.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. namespace app\controller;
  3. use app\BaseController;
  4. use think\facade\Db;
  5. use \app\common\util\http;
  6. class Sms extends BaseController
  7. {
  8. protected $users = [
  9. [
  10. 'keyID' => '123456',
  11. 'keySecret' => '',
  12. ]
  13. ];
  14. /**
  15. * 未做,日志记录,出错记录等
  16. *
  17. * 格式
  18. POST https://stest.uzipm.com/sms/send HTTP/1.1
  19. User-Agent: okhttp/2.7.5
  20. Accept-Encoding: gzip
  21. Connection: Keep-Alive
  22. Host: stest.uzipm.com
  23. Content-Length: 82
  24. Content-Type: application/json; charset=utf-8
  25. {"senderid":"MiChat","to":"6590511066","countryID":"SG","content":"test from gzh"}
  26. * 未做,日志记录,出错记录等
  27. */
  28. function send(){
  29. $input = file_get_contents("php://input");
  30. $input = json_decode($input,true);
  31. $countryID = $input['countryID']; //CN,SG
  32. $phoneNumber = $input['to'];
  33. $content = $input['content'];
  34. $param['user']=$content;
  35. $params['smsTemplateId'] = '0a2dc8e2-0f64-4b41-a10c-50222d8b6e65';
  36. $params['paramValues'] = $param; // paramValues:{"user":"XXX"}
  37. $params['msisdn'] = $phoneNumber;
  38. $params['countryID'] = $countryID;
  39. $url = "https://www.huaweiita.com:17131/apiaccess/sms/sendTemplateSms/v1";
  40. $retCurl = curl_huawei_sms($url, json_encode($params));
  41. $retCurl = json_decode($retCurl,true); //原始信息入库
  42. if($retCurl['code'] == '0000000'){
  43. $ret['code'] = 1;
  44. $ret['msg'] = $retCurl['description'];
  45. }else{
  46. $ret['code'] = 0;
  47. $ret['msg'] = $retCurl['description'];
  48. $ret['error_code'] = $retCurl['code'];
  49. $ret['error_msg'] = $retCurl['exErrorMessage'];
  50. //记录原始错误消息入库
  51. }
  52. }
  53. /**
  54. * $templateId
  55. * $param
  56. * $phone_number
  57. * countryID
  58. */
  59. function sendByAuth()
  60. {
  61. $headerAuthorization = I('server.HTTP_AUTHORIZATION');
  62. $headerAuthorization = str_replace('"', '', $headerAuthorization);
  63. $headerAuthorization = explode(',', $headerAuthorization);
  64. $headerAuthorization = array_map(function ($v) {
  65. return trim($v);
  66. }, $headerAuthorization);
  67. parse_str(implode('&', $headerAuthorization), $headerAuthorization);
  68. $postDta = file_get_contents('php://input');
  69. $postDta = json_decode($postDta, true);
  70. $postNonce = $headerAuthorization['nonce'];
  71. $postTime = $headerAuthorization['time'];
  72. $postKeyID = $headerAuthorization['keyID'];
  73. $postSign = $headerAuthorization['sign'];
  74. $dbUserInfo = Db::name('user')->where(['key_id' => $headerAuthorization['keyID']])->find();
  75. $param = $postDta['paramValues'];
  76. $dbUserInfo = [];
  77. $dbUserInfo['key_id'] = "123456";
  78. $dbUserInfo['key_secret'] = "12345678";
  79. $ret = [];
  80. $sign = $this->makeSign($postNonce, $postTime, $dbUserInfo['key_secret']);
  81. if ($sign != $postSign) {
  82. $templateId = I('template_id');
  83. $templateId = "sms_123456";
  84. $phoneNumber = I('phoneNumber'); //8615812345678
  85. $templateContent = '短信内容 {$content} [test]';
  86. $smsContent = str_replace('{$user}', $param['user'], $templateContent);
  87. $countryID = I('countryID');
  88. $countryID = 'CN';
  89. $url = "https://www.huaweiita.com:17131/apiaccess/sms/sendTemplateSms/v1";
  90. $params['smsTemplateId'] = '695db7b5-6d4f-4791-aea0-f733f2ef06ce';
  91. $params['paramValues'] = $param; // paramValues:{"user":"XXX"}
  92. $params['msisdn'] = $phoneNumber;
  93. $params['countryID'] = $countryID;
  94. $params['operatorCode'] = $postDta['operatorCode'];
  95. $retCurl = curl_huawei_sms($url, json_encode($params));
  96. $retCurl = json_decode($retCurl,true); //原始信息入库
  97. if($retCurl['code'] == '0000000'){
  98. $ret['code'] = 1;
  99. $ret['msg'] = $retCurl['description'];
  100. }else{
  101. $ret['code'] = 0;
  102. $ret['msg'] = $retCurl['description'];
  103. $ret['error_code'] = $retCurl['code'];
  104. $ret['error_msg'] = $retCurl['exErrorMessage'];
  105. //记录原始错误消息入库
  106. }
  107. //var_dump($ret);exit('sb');
  108. } else {
  109. $ret = [];
  110. $ret['code'] = 0;
  111. $ret['msg'] = 'authentication failure';
  112. //return $ret;
  113. //echo json_encode($ret);
  114. // exit;
  115. }
  116. return $ret;
  117. //$sign = "";
  118. }
  119. function makeSign($nonce, $time, $key)
  120. {
  121. $signRawString = $nonce . $time . $key;
  122. return base64_encode(hash('sha256', $signRawString));
  123. }
  124. /**
  125. *
  126. * 检测签名
  127. */
  128. public function CheckSign()
  129. {
  130. $sign = $this->MakeSign();
  131. if ($this->GetSign() == $sign) {
  132. return true;
  133. }
  134. throw new WxPayException("签名错误!");
  135. }
  136. function sendToH3c()
  137. {
  138. $url = "https://www.huaweiita.com:17131/apiaccess/sms/sendTemplateSms/v1";
  139. $params = [];
  140. //$params['smsTemplateId'] = '695db7b5-6d4f-4791-aea0-f733f2ef06ce';
  141. $params['smsTemplateId'] = '0a2dc8e2-0f64-4b41-a10c-50222d8b6e65';
  142. $paramValues = [];
  143. $paramValues['user'] = 'test1';
  144. $params['paramValues'] = $paramValues; // paramValues:{"user":"XXX"}
  145. $params['msisdn'] = '8615821868265';
  146. $params['countryID'] = 'SG';
  147. //$params['msisdn'] = '8613162836361';
  148. //$params['countryID'] = 'CN';
  149. $ret = curl_huawei_sms($url, json_encode($params));
  150. }
  151. function sendByGuzzle()
  152. {
  153. $url = "https://www.huaweiita.com:17131/apiaccess/sms/sendTemplateSms/v1";
  154. //$url = "http://stest.uzipm.com/index/index";
  155. $params = [];
  156. $params['smsTemplateId'] = '695db7b5-6d4f-4791-aea0-f733f2ef06ce';
  157. $params['smsTemplateId'] = '0a2dc8e2-0f64-4b41-a10c-50222d8b6e65';
  158. $paramValues = [];
  159. $paramValues['user'] = 'test1';
  160. $params['paramValues'] = $paramValues; // paramValues:{"user":"XXX"}
  161. $params['msisdn'] = '8615821868265';
  162. $params['countryID'] = 'SG';
  163. // $params['msisdn'] = '8613162836361';
  164. // $params['countryID'] = 'CN';
  165. //$ret = curl_post_json($url, json_encode($params));
  166. $header = $this->getHeader();
  167. $http = new http();
  168. $http->request($url,'post',json_encode($params),$header);
  169. }
  170. function getHeader(){
  171. date_default_timezone_set("UTC");
  172. //$nonce = '66C92B11FF8A425FB8D4CCFE';
  173. $nonce = $nonce1 = mt_rand(100000,9999999999);
  174. $time = time();
  175. $created = date('Y-m-d', $time) . 'T' . date('H:i:s', $time) . 'Z';
  176. //$created = '2019-07-10T15:02:08Z';
  177. //test
  178. $username = 'd9c3190120db4b2ab078a543d59ba47d';
  179. $password = '33ab0a75ee246f85e41f2c15d867ca5d';
  180. //正式
  181. $username='67ef6805e6004cce9cce24b7f13767c6';
  182. $password = '1e88f55b4e034b1783e2686ec9dfbffd';
  183. $nonce = base64_encode($nonce);// . $created . $password;
  184. $signRawString = $nonce . $created . $password;//;//
  185. echo "明文:", $signRawString,"\n";
  186. echo "明文:", $signRawString,"\n";
  187. //$signRawString = '66C92B11FF8A425FB8D4CCFE2019-07-08T17:56:46Z33ab0a75ee246f85e41f2c15d867ca5d';
  188. //$signRawString='a';
  189. //var_dump(hash('sha256',$signRawString,true));exit;
  190. $passwordDigest = base64_encode(hash('sha256',$signRawString,true));
  191. //echo "明文: $signRawString \n";
  192. //echo "密文: $passwordDigest \n";
  193. //exit;
  194. $headerArr=[];
  195. $headerArr['Authorization'] = 'WSSE realm="SDP", profile="UsernameToken", type="Appkey"';
  196. $headerArr['X-WSSE'] = 'UsernameToken Username="'.$username.'",PasswordDigest="' . $passwordDigest . '",Nonce="' . $nonce . '",Created="' . $created . '"';
  197. $headerArr['Content-Type'] = 'application/json; charset=UTF-8';
  198. return $headerArr;
  199. }
  200. }