<?php
/**
 * [UQcms System] Copyright uqcms.com
 * QQ: 2200631718
 * Author: gj.name
 * This is NOT a freeware
*/  class sms_qcloud { function __construct($app_id, $app_key){ $this->app_id = $app_id; $this->app_key = $app_key; $this->url = 'https://yun.tim.qq.com/v5/tlssmssvr/sendsms'; } function send($mobile, $param_arr, $tpl_id, $sign = null){ $random = $this->getRandom(); $curTime = time(); $wholeUrl = $this->url."?sdkappid=".$this->app_id."&random=".$random; $data = new \stdClass(); $tel = new \stdClass(); $tel->nationcode = "86"; $tel->mobile = $mobile; $data->tel = $tel; $data->params = $this->param($param_arr); $data->sig = $this->calculateSigForTempl($this->app_key, $random, $curTime, $mobile); $data->tpl_id = (int)$tpl_id; $data->sign = ''; $data->time = $curTime; $data->extend = ''; $data->ext = ''; $json_data = json_encode($data); $result = curl::post($wholeUrl, $json_data); $result_data = json_decode($result); if($result_data->result == '0'){ $result_info['error'] = '0'; $result_info['msg'] = '发送成功'; return $result_info; }else{ $result_info['error'] = '1'; $error_name = $this->error_code($result_data->result); if($error_name){ $result_info['msg'] = $error_name; }else{ sys_log('腾讯短信错误代码：'.$result); $result_info['msg'] = '发送失败，查看系统日志。'; } return $result_info; } } function param($param_arr){ foreach($param_arr as $v){ $arr[] = $v; } return $arr; } function calculateSigForTempl($appkey, $random, $curTime, $phoneNumber) { $phoneNumbers = array($phoneNumber); return $this->calculateSigForTemplAndPhoneNumbers($appkey, $random, $curTime, $phoneNumbers); } function calculateSigForTemplAndPhoneNumbers($appkey, $random, $curTime, $phoneNumbers) { $phoneNumbersString = $phoneNumbers[0]; for($i = 1; $i < count($phoneNumbers); $i++){ $phoneNumbersString .= ("," . $phoneNumbers[$i]); } return hash("sha256", "appkey=".$appkey."&random=".$random."&time=".$curTime."&mobile=".$phoneNumbersString); } function getRandom() { return rand(100000, 999999); } function error_code($code){ $codeArr = array( '1001'=>'sig校验失败', '1002'=>'短信/语音内容中含有敏感词', '1004'=>'请求包解析失败，通常情况下是由于没有遵守API接口说明规范导致的', '1007'=>'其他错误', '1009'=>'请求ip不在白名单中', '1012'=>'签名格式错误或者签名未审批', '1014'=>'模版未审批或请求的内容与审核通过的模版内容不匹配', '1015'=>'手机号在黑名单库中,通常是用户退订或者命中运营商黑名单导致的', '1016'=>'手机号格式错误', '1017'=>'请求的短信内容太长', '1021'=>'请求发起时间不正常，通常是由于您的服务器时间与腾讯云服务器时间差异超过10分钟导致的', '1022'=>'业务短信日下发条数超过设定的上限', '1023'=>'单个手机号30秒内下发短信条数超过设定的上限', '1024'=>'单个手机号1小时内下发短信条数超过设定的上限', '1025'=>'单个手机号日下发短信条数超过设定的上限', '1026'=>'单个手机号下发相同内容超过设定的上限' ); if(isset($codeArr[$code])){ return $codeArr[$code]; }else{ return false; } } private static $instance; public static function init(){ if (is_null(self::$instance)){ self::$instance = new self(); } return self::$instance; } }