<?php
/** 
 * UQ云商：	www.uqcms.com  
 * 联系QQ：	2200631718 
 * 可免费商用，需要留UQ云商链接作为交换，只首页底部留取即可，不影响使用。
 */
  class sms_aliyun { private static $uq0; public static function init() { if (is_null(self::$uq0)){ self::$uq0 = new self(); } return self::$uq0; } function __construct($uq1) { $this->url = 'https://dysmsapi.aliyuncs.com/'; $this->app_id = $uq1['app_id']; $this->app_key = $uq1['app_key']; $this->sign = $uq1['sign']; } function send($uq2, $uq3, $uq4) { $uq5['RegionId'] = 'cn-hangzhou'; $uq5['Action'] = 'SendSms'; $uq5['SignName'] = $this->sign; $uq5['TemplateCode'] = $uq3; $uq5['PhoneNumbers'] = $uq2; $uq5['TemplateParam'] = json_encode($uq4); $uq6 = $this->result_data($uq5); $uq7['x-sdk-client'] = "php/2.0.0"; $uq8 = curl::post($this->url, $uq6, $uq7); $uq9 = json_decode($uq8); if($uq9->Message == 'OK'){ return ['error'=>'0', 'msg'=>'发送成功']; }else{ $uq10 = $this->error_code($uq9->Code); if($uq10){ $uq11 = $uq10; }else{ $uq11 = $uq9->Code; } return array('error'=>'1', 'msg'=>$uq11); } } function result_data($uq5 = array()) { $uq12 = $uq5; $uq12['Format'] = 'JSON'; date_default_timezone_set("GMT"); $uq12['Version'] = '2017-05-25'; $uq12['SignatureMethod'] = 'HMAC-SHA1'; $uq12['SignatureNonce'] = uniqid(); $uq12['SignatureVersion'] = '1.0'; $uq12['AccessKeyId'] = $this->app_id; $uq12['Timestamp'] = date('Y-m-d\TH:i:s\Z'); $uq12['Signature'] = $this->computeSignature($uq12, $this->app_key); return $uq12; } private function computeSignature($uq13, $uq14) { ksort($uq13); $uq15 = ''; foreach($uq13 as $uq16 => $uq17){ $uq15 .= '&'.$this->percentEncode($uq16).'='.$this->percentEncode($uq17); } $uq18 = 'POST&%2F&'.$this->percentencode(substr($uq15, 1)); return $this->signString($uq18, $uq14."&"); } protected function percentEncode($uq19) { $uq20 = urlencode($uq19); $uq20 = preg_replace('/\+/', '%20', $uq20); $uq20 = preg_replace('/\*/', '%2A', $uq20); $uq20 = preg_replace('/%7E/', '~', $uq20); return $uq20; } function signString($uq18, $uq14) { return base64_encode(hash_hmac('sha1', $uq18, $uq14, true)); } function error_code($uq21) { $uq22 = [ 'SignatureDoesNotMatch'=>'签名不通过', 'Throttling.User'=>'请稍后访问，降低访问频率', 'MissingSecurityToken'=>'请检查是否传入有效AccessKeyId', 'isv.OUT_OF_SERVICE'=>'短信欠费', 'Throttling.API'=>'请稍后访问，降低访问频率' ]; if(isset($uq22[$uq21])){ return $uq22[$uq21]; }else{ return false; } } } 