<?php
/**
 * [UQcms System] Copyright uqcms.com
 * QQ: 2200631718
 * Author: gj.name
 * This is NOT a freeware
*/  class sms_aliyun { private $dateTimeFormat = 'Y-m-d\TH:i:s\Z'; function __construct($app_id, $app_key){ $this->app_id = $app_id; $this->app_key = $app_key; $this->url = 'https://dysmsapi.aliyuncs.com/'; } function send($mobile, $param_arr, $tpl_id, $sign){ $apiArr['Action'] = 'SingleSendSms'; $apiArr['SignName'] = $sign; $apiArr['TemplateCode'] = $tpl_id; $apiArr['RecNum'] = $mobile; $apiArr['RecNum'] = $mobile; $apiArr['ParamString'] = json_encode($param_arr); $postUrl = $this->result_data($apiArr); $result = curl::get($postUrl); $result_json = json_decode($result); if(isset($result_json->Model)){ $result_info['error'] = '0'; $result_info['msg'] = '发送成功'; return $result_info; }else{ $result_info['error'] = '1'; $error_name = $this->error_code($result_json->Code); if($error_name){ $result_info['msg'] = $error_name; }else{ sys_log('阿里短信错误代码：'.$result); $result_info['msg'] = '发送失败，查看系统日志。'; } return $result_info; } } function result_data($apiArr = array()){ $post_arr = $apiArr; $post_arr['Format'] = 'JSON'; date_default_timezone_set("GMT"); $post_arr['Version'] = '2016-09-27'; $post_arr['SignatureMethod'] = 'HMAC-SHA1'; $post_arr['SignatureNonce'] = uniqid(); $post_arr['SignatureVersion'] = '1.0'; $post_arr['AccessKeyId'] = $this->app_id; $post_arr['Timestamp'] = date($this->dateTimeFormat); $post_arr['Signature'] = $this->computeSignature($post_arr, $this->app_key); $requestUrl = $this->url."?"; foreach ($post_arr as $key=>$val){ $requestUrl .= $key."=".urlencode($val)."&"; } return substr($requestUrl, 0, -1); } private function computeSignature($parameters, $accessKeySecret){ ksort($parameters); $queryString = ''; foreach($parameters as $key => $value){ $queryString .= '&' . $this->percentEncode($key). '=' . $this->percentEncode($value); } $stringToSign = 'GET&%2F&' . $this->percentencode(substr($queryString, 1)); $signature = $this->signString($stringToSign, $accessKeySecret."&"); return $signature; } protected function percentEncode($str){ $res = urlencode($str); $res = preg_replace('/\+/', '%20', $res); $res = preg_replace('/\*/', '%2A', $res); $res = preg_replace('/%7E/', '~', $res); return $res; } function signString($stringToSign, $accessKeySecret){ return base64_encode(hash_hmac('sha1', $stringToSign, $accessKeySecret, true)); } function error_code($code){ $codeArr = array( 'InvalidDayuStatus.Malformed'=>'账户短信开通状态不正确', 'InvalidSignName.Malformed'=>'短信签名不正确或签名状态不正确', 'InvalidTemplateCode.MalFormed'=>'短信模板Code不正确或者模板状态不正确', 'InvalidRecNum.Malformed'=>'目标手机号不正确，单次发送数量不能超过100', 'InvalidParamString.MalFormed'=>'短信模板中变量不是json格式', 'InvalidParamStringTemplate.Malformed'=>'短信模板中变量与模板内容不匹配', 'InvalidSendSms'=>'触发业务流控', 'InvalidDayu.Malformed'=>'变量不能是url，可以将变量固化在模板中', 'InvalidAccessKeyId'=>'检查AccessKeyId是否正确', 'MissingReceiptHandle'=>'请求缺少参数', 'MissingVisibilityTimeout'=>'请求缺少参数', 'InvalidArgument'=>'消息体过长' ); 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; } }