<?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['RegionId'] = 'cn-hangzhou'; $apiArr['Action'] = 'SendSms'; $apiArr['SignName'] = $sign; $apiArr['TemplateCode'] = $tpl_id; $apiArr['PhoneNumbers'] = $mobile; $apiArr['TemplateParam'] = json_encode($param_arr); $postData = $this->result_data($apiArr); $header['x-sdk-client'] = "php/2.0.0"; $result = curl::post($this->url, $postData, $header); $rjson = json_decode($result); if($rjson->Message == 'OK'){ return array('error'=>'0', 'msg'=>'发送成功'); }else{ $error_name = $this->error_code($rjson->Code); if($error_name){ $error_msg = $error_name; }else{ $error_msg = $rjson->Code; } return array('error'=>'1', 'msg'=>$error_msg); } } function result_data($apiArr = array()){ $post_arr = $apiArr; $post_arr['Format'] = 'JSON'; date_default_timezone_set("GMT"); $post_arr['Version'] = '2017-05-25'; $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); return $post_arr; } private function computeSignature($parameters, $accessKeySecret){ ksort($parameters); $queryString = ''; foreach($parameters as $key => $value){ $queryString .= '&'.$this->percentEncode($key).'='.$this->percentEncode($value); } $stringToSign = 'POST&%2F&'.$this->percentencode(substr($queryString, 1)); return $this->signString($stringToSign, $accessKeySecret."&"); } 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( 'SignatureDoesNotMatch'=>'签名不通过', 'Throttling.User'=>'请稍后访问，降低访问频率', 'MissingSecurityToken'=>'请检查是否传入有效AccessKeyId', 'Throttling.API'=>'请稍后访问，降低访问频率' ); 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; } }