Sms.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace ums;
  3. class Sms
  4. {
  5. private $spCode = '';// 企业编号
  6. private $appKey = '';// 开户时的管理员帐号
  7. private $appSecret = '';//密钥
  8. public function __construct()
  9. {
  10. $config = sysconfig('sms');
  11. if(!empty($config)){
  12. if(1!=$config['send_switch']){
  13. return false;
  14. }else{
  15. $this->spCode = !empty($config['ums_spCode'])?$config['ums_spCode']:$this->spCode;
  16. $this->appKey = !empty($config['ums_appKey'])?$config['ums_appKey']:$this->appKey;
  17. $this->appSecret = !empty($config['ums_appSecret'])?$config['ums_appSecret']:$this->appSecret;
  18. }
  19. }
  20. }
  21. /**
  22. * @title: 发送模板短信
  23. * @desc:
  24. * @param {string} {mobile} {} {手机号}
  25. * @param {string} {content} {} {短信内容}
  26. * @param {string} {tpl_id} {} {短信模板ID}
  27. * @return {*}
  28. * @Author: Rock
  29. * @Date: 2022-03-12 10:21:23
  30. * @LastEditTime: Do not edit
  31. */
  32. public function SendSms1($mobile='',$content='',$tpl_id='')
  33. {
  34. try{
  35. $url = "https://api.ums86.com:9600/sms/Api/Send.do";
  36. $data = [
  37. 'SpCode' => $this->spCode,
  38. 'LoginName' => $this->appKey,
  39. 'Password' => $this->appSecret,
  40. 'UserNumber' => $mobile,
  41. 'MessageContent'=> iconv( "UTF-8", "gb2312//TRANSLIT//IGNORE" , $content),// 部分中文使用iconv转换会导致文字丢失,改用下面的方法SendSms,使用mb_convert_encoding
  42. 'templateId' => $tpl_id,
  43. 'SerialNumber' => $this->serialNumber(),
  44. 'f' => 1
  45. ];
  46. $res = self::PostUrl($data,$url);
  47. if($res['result']!=0){
  48. $data['MessageContent'] = iconv("gb2312","UTF-8",$data['MessageContent']);// 部分中文使用iconv转换会导致文字丢失,改用下面的方法SendSms,使用mb_convert_encoding
  49. WLog('ums_sms_log','MOBILES:'.$mobile);
  50. WLog('ums_sms_log','REQUEST:'.json_encode($data,JSON_UNESCAPED_UNICODE));
  51. WLog('ums_sms_log','RESPONSE:'.json_encode($res,JSON_UNESCAPED_UNICODE));
  52. }
  53. $res['serialNumber'] = $data['SerialNumber'];
  54. return $res;
  55. }catch(\Exception $e){
  56. WLog('ums_sms_log','ERROR:'.$e->getFile().$e->getLine().$e->getMessage());
  57. WLog('ums_sms_log','CONTENT:'.$content);
  58. WLog('ums_sms_log','DATA:'.json_encode($data));
  59. }
  60. }
  61. public function SendSms($mobile='',$content='',$tpl_id='')
  62. {
  63. try{
  64. $url = "https://api.ums86.com:9600/sms/Api/Send.do";
  65. $data = [
  66. 'SpCode' => $this->spCode,
  67. 'LoginName' => $this->appKey,
  68. 'Password' => $this->appSecret,
  69. 'UserNumber' => $mobile,
  70. 'MessageContent'=> mb_convert_encoding( $content,"GBK", "UTF-8,GBK,GB2312,BIG5"),
  71. 'templateId' => $tpl_id,
  72. 'SerialNumber' => $this->serialNumber(),
  73. 'f' => 1
  74. ];
  75. $res = self::PostUrl($data,$url);
  76. if($res['result']!=0){
  77. $data['MessageContent'] = mb_convert_encoding($data['MessageContent'],"UTF-8","UTF-8,GBK,GB2312,BIG5");
  78. WLog('ums_sms_log','MOBILES:'.$mobile);
  79. WLog('ums_sms_log','REQUEST:'.json_encode($data,JSON_UNESCAPED_UNICODE));
  80. WLog('ums_sms_log','RESPONSE:'.json_encode($res,JSON_UNESCAPED_UNICODE));
  81. }
  82. $res['serialNumber'] = $data['SerialNumber'];
  83. return $res;
  84. }catch(\Exception $e){
  85. WLog('ums_sms_log','ERROR:'.$e->getFile().$e->getLine().$e->getMessage());
  86. WLog('ums_sms_log','CONTENT:'.$content);
  87. WLog('ums_sms_log','DATA:'.json_encode($data));
  88. }
  89. }
  90. /**
  91. * @title: 发送POST请求
  92. * @desc:
  93. * @param {array} {data} {} {请求数据}
  94. * @param {string} {url} {} {请求地址}
  95. * @param {array} {header} {} {请求头}
  96. * @return {*}
  97. * @Author: Rock
  98. * @Date: 2022-03-12 15:29:56
  99. * @LastEditTime: Do not edit
  100. */
  101. private function PostUrl($data,$url,$header=[])
  102. {
  103. try{
  104. $data = is_string($data)?$data:http_build_query($data);
  105. $ch = curl_init();
  106. curl_setopt($ch, CURLOPT_TIMEOUT, 30);//设置超时
  107. curl_setopt($ch,CURLOPT_URL, $url);
  108. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
  109. curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);//严格校验
  110. curl_setopt($ch, CURLOPT_HEADER, FALSE);//设置header
  111. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);//要求结果为字符串且输出到屏幕上
  112. curl_setopt($ch, CURLOPT_POST, TRUE);//post提交方式
  113. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  114. $data = curl_exec($ch);//运行curl
  115. //返回结果
  116. if($data){
  117. curl_close($ch);
  118. $result = explode('&',$data);
  119. $res = [];
  120. foreach($result as $item){
  121. $itemAry = explode('=',$item);
  122. $res[$itemAry[0]] = iconv('GB2312','UTF-8',$itemAry[1]);
  123. }
  124. return $res;
  125. } else {
  126. $error = curl_errno($ch);
  127. curl_close($ch);
  128. WLog('Post_Curl_Error',"CURL出错,错误码:$error"."URL:$url");
  129. }
  130. }catch(Exception $e){
  131. WLog('Post_Curl_Error',"CURL出错:".$e->getFile().'第'.$e->getLine().'行:'.$e->getMessage());
  132. }
  133. }
  134. // 生成流水号
  135. static function serialNumber()
  136. {
  137. $microtime = microtime();
  138. $ary = explode(' ',$microtime);
  139. $timestamp = intval((floatval($ary[1])+floatval($ary[0]))*1000);
  140. $random = mt_rand(000000000,999999999);
  141. return $timestamp.$random;
  142. }
  143. }