Sms.php 8.0 KB

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