123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php
- namespace ums;
- class Sms
- {
- private $spCode = '';// 企业编号
- private $appKey = '';// 开户时的管理员帐号
- private $appSecret = '';//密钥
- public function __construct()
- {
- $config = sysconfig('sms');
- if(!empty($config)){
- if(1!=$config['send_switch']){
- return false;
- }else{
- $this->spCode = !empty($config['ums_spCode'])?$config['ums_spCode']:$this->spCode;
- $this->appKey = !empty($config['ums_appKey'])?$config['ums_appKey']:$this->appKey;
- $this->appSecret = !empty($config['ums_appSecret'])?$config['ums_appSecret']:$this->appSecret;
- }
- }
- }
- /**
- * @title: 发送模板短信
- * @desc:
- * @param {string} {mobile} {} {手机号}
- * @param {string} {content} {} {短信内容}
- * @param {string} {tpl_id} {} {短信模板ID}
- * @return {*}
- * @Author: Rock
- * @Date: 2022-03-12 10:21:23
- * @LastEditTime: Do not edit
- */
- public function SendSms1($mobile='',$content='',$tpl_id='')
- {
- try{
- $url = "https://api.ums86.com:9600/sms/Api/Send.do";
- $data = [
- 'SpCode' => $this->spCode,
- 'LoginName' => $this->appKey,
- 'Password' => $this->appSecret,
- 'UserNumber' => $mobile,
- 'MessageContent'=> iconv( "UTF-8", "gb2312//TRANSLIT//IGNORE" , $content),// 部分中文使用iconv转换会导致文字丢失,改用下面的方法SendSms,使用mb_convert_encoding
- 'templateId' => $tpl_id,
- 'SerialNumber' => $this->serialNumber(),
- 'f' => 1
- ];
- $res = self::PostUrl($data,$url);
- if($res['result']!=0){
- $data['MessageContent'] = iconv("gb2312","UTF-8",$data['MessageContent']);// 部分中文使用iconv转换会导致文字丢失,改用下面的方法SendSms,使用mb_convert_encoding
- WLog('ums_sms_log','MOBILES:'.$mobile);
- WLog('ums_sms_log','REQUEST:'.json_encode($data,JSON_UNESCAPED_UNICODE));
- WLog('ums_sms_log','RESPONSE:'.json_encode($res,JSON_UNESCAPED_UNICODE));
- }
- $res['serialNumber'] = $data['SerialNumber'];
- return $res;
- }catch(\Exception $e){
- WLog('ums_sms_log','ERROR:'.$e->getFile().$e->getLine().$e->getMessage());
- WLog('ums_sms_log','CONTENT:'.$content);
- WLog('ums_sms_log','DATA:'.json_encode($data));
- }
- }
- public function SendSms($mobile='',$content='',$tpl_id='')
- {
- try{
- $url = "https://api.ums86.com:9600/sms/Api/Send.do";
- $data = [
- 'SpCode' => $this->spCode,
- 'LoginName' => $this->appKey,
- 'Password' => $this->appSecret,
- 'UserNumber' => $mobile,
- 'MessageContent'=> mb_convert_encoding( $content,"GBK", "UTF-8,GBK,GB2312,BIG5"),
- 'templateId' => $tpl_id,
- 'SerialNumber' => $this->serialNumber(),
- 'f' => 1
- ];
- $res = self::PostUrl($data,$url);
- if($res['result']!=0){
- $data['MessageContent'] = mb_convert_encoding($data['MessageContent'],"UTF-8","UTF-8,GBK,GB2312,BIG5");
- WLog('ums_sms_log','MOBILES:'.$mobile);
- WLog('ums_sms_log','REQUEST:'.json_encode($data,JSON_UNESCAPED_UNICODE));
- WLog('ums_sms_log','RESPONSE:'.json_encode($res,JSON_UNESCAPED_UNICODE));
- }
- $res['serialNumber'] = $data['SerialNumber'];
- return $res;
- }catch(\Exception $e){
- WLog('ums_sms_log','ERROR:'.$e->getFile().$e->getLine().$e->getMessage());
- WLog('ums_sms_log','CONTENT:'.$content);
- WLog('ums_sms_log','DATA:'.json_encode($data));
- }
- }
- /**
- * @title: 发送POST请求
- * @desc:
- * @param {array} {data} {} {请求数据}
- * @param {string} {url} {} {请求地址}
- * @param {array} {header} {} {请求头}
- * @return {*}
- * @Author: Rock
- * @Date: 2022-03-12 15:29:56
- * @LastEditTime: Do not edit
- */
- private function PostUrl($data,$url,$header=[])
- {
- try{
- $data = is_string($data)?$data:http_build_query($data);
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_TIMEOUT, 30);//设置超时
- curl_setopt($ch,CURLOPT_URL, $url);
- curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
- curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);//严格校验
- curl_setopt($ch, CURLOPT_HEADER, FALSE);//设置header
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);//要求结果为字符串且输出到屏幕上
- curl_setopt($ch, CURLOPT_POST, TRUE);//post提交方式
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- $data = curl_exec($ch);//运行curl
- //返回结果
- if($data){
- curl_close($ch);
- $result = explode('&',$data);
- $res = [];
- foreach($result as $item){
- $itemAry = explode('=',$item);
- $res[$itemAry[0]] = iconv('GB2312','UTF-8',$itemAry[1]);
- }
- return $res;
- } else {
- $error = curl_errno($ch);
- curl_close($ch);
- WLog('Post_Curl_Error',"CURL出错,错误码:$error"."URL:$url");
- }
- }catch(Exception $e){
- WLog('Post_Curl_Error',"CURL出错:".$e->getFile().'第'.$e->getLine().'行:'.$e->getMessage());
- }
- }
- // 生成流水号
- static function serialNumber()
- {
- $microtime = microtime();
- $ary = explode(' ',$microtime);
- $timestamp = intval((floatval($ary[1])+floatval($ary[0]))*1000);
- $random = mt_rand(000000000,999999999);
- return $timestamp.$random;
- }
- }
|