阿里云短信接口
配置文件
config.php
1//阿里大鱼 2 'Ali_SMS' =>array( 3 'sms_temp' =>'短信模板', 4 'sms_sign' =>'签名', 5 'appkey' =>'appkey', 6 'secretKey'=>'secretKey', 7 ),
TestController.php
1Vendor('alisms.Alisms'); 2 $alisms = new \Alisms(C('Ali_SMS.appkey'),C('Ali_SMS.secretKey')); 3 $mobile = $phone; 4 $temp_code = C('Ali_SMS.sms_temp'); 5 6 $paramString = '{"code":"'.$code.'"}'; 7 $alisms->signName = C('Ali_SMS.sms_sign'); 8 $re = $alisms->smsend($mobile,$temp_code,$paramString); 9 if($re['Code'] =='OK'){ 10 $info['status'] = 1; 11 $info['info'] = '短信发送成功!'; 12 echo json_encode($info); 13 exit; 14 }else{ 15 $info['info'] = '短信发送失败'; 16 $info['status'] = 0; 17 echo json_encode($info); 18 exit; 19 }
Alisms.php
1<?php 2 3/** 4 * 阿里云短信接口 5 * @author 墨白<453885726@qq.com> 6 * 示例 7 * $alisms = new \Common\Model\Alisms($accessKeyId,$accessKeySecret); 8 * $mobile = '18788830181'; 9 * $code = 'SMS_36225243'; 10 * $paramString = '{"code":"344556"}'; 11 * $re = $alisms->smsend($mobile,$code,$paramString); 12 * print_r($re); 13 * 14 */ 15 16class Alisms{ 17 public $config = array( 18 'Format' =>'json', //返回值的类型,支持JSON与XML。默认为XML 19 'Version' =>'2017-05-25', //API版本号,为日期形式:YYYY-MM-DD,本版本对应为2016-09-27 20 'SignatureMethod' =>'HMAC-SHA1', //签名方式,目前支持HMAC-SHA1 21 'SignatureVersion'=>'1.0', 22 ); 23 private $accessKeySecret; 24 private $http = 'http://dysmsapi.aliyuncs.com';//https://sms.aliyuncs.com/'; //短信接口 25 private $dateTimeFormat = 'Y-m-d\TH:i:s\Z'; 26 27 public $signName = '短信签名'; //管理控制台中配置的短信签名(状态必须是验证通过) 28 public $method = 'GET'; 29 /** 30 *发送短信 31 *@AccessKeyId 阿里云申请的 Access Key ID 32 *@AccessKeySecret 阿里云申请的 Access Key Secret 33 */ 34 function __construct($accessKeyId,$accessKeySecret){ 35 $this->config['AccessKeyId'] = $accessKeyId; 36 $this->AccessKeySecret = $accessKeySecret; 37 } 38 /** 39 *发送短信 40 *@mobile 目标手机号,多个手机号可以逗号分隔 41 *@code 短信模板的模板CODE 42 *@ParamString 短信模板中的变量;,参数格式{“no”:”123456”}, 个人用户每个变量长度必须小于15个字符 43 */ 44 public function smsend($mobile,$code,$ParamString){ 45 $apiParams = $this->config; 46 $apiParams["Action"] = 'SendSms';//'SingleSendSms'; 47 $apiParams['TemplateCode'] = $code; //短信模板的模板CODE 48 //$apiParams['RecNum'] = $mobile; //目标手机号,多个手机号可以逗号分隔 49 //$apiParams['ParamString'] = $ParamString; //短信模板中的变量;,此参数传递{“no”:”123456”}, 个人用户每个变量长度必须小于15个字符 50 $apiParams['SignName'] = $this->signName; //管理控制台中配置的短信签名(状态必须是验证通过) 51 date_default_timezone_set("GMT"); 52 $apiParams["Timestamp"] = date($this->dateTimeFormat); 53 $apiParams["SignatureNonce"] = md5(md5('wbh').rand(100000,999999).uniqid()); //唯一随机数 54 55 $apiParams['RegionId'] = 'cn-hangzhou'; 56 $apiParams['PhoneNumbers'] = $mobile; 57 $apiParams['TemplateParam'] = $ParamString; 58 59 $apiParams["Signature"] = $this->computeSignature($apiParams, $this->AccessKeySecret);//签名 60 61 $tag = '?'; 62 $requestUrl = $this->http; 63 foreach ($apiParams as $apiParamKey => $apiParamValue){ 64 $requestUrl .= $tag."$apiParamKey=" . urlencode($apiParamValue); 65 $tag = '&'; 66 } 67 return $this->postSMS($requestUrl); 68 } 69 private function postSMS($url){ 70 $ch = curl_init(); 71 curl_setopt($ch, CURLOPT_URL, $url); 72 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 73 curl_setopt($ch, CURLOPT_HEADER, 0); 74 $output = curl_exec($ch); 75 curl_close($ch); 76 return json_decode($output,true); 77/* 78 $opts = array( 79 'http'=>array( 80 'method'=>$this->method, 81 'timeout'=>600, 82 'header'=>'Content-Type: application/x-www-form-urlencoded', 83 ) 84 ); 85 $html = file_get_contents($url, false, stream_context_create($opts)); 86 if($html){ 87 return json_decode($html,true); 88 }else{ 89 return false; 90 }*/ 91 } 92 93 //生成取短信签名 94 private function computeSignature($parameters, $accessKeySecret){ 95 ksort($parameters); 96 $canonicalizedQueryString = ''; 97 foreach($parameters as $key => $value){ 98 $canonicalizedQueryString .= '&' . $this->percentEncode($key). '=' . $this->percentEncode($value); 99 } 100 $stringToSign = $this->method.'&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1)); 101 $signature = $this->signString($stringToSign, $accessKeySecret."&"); 102 return $signature; 103 } 104 protected function percentEncode($str){ 105 $res = urlencode($str); 106 $res = preg_replace('/\+/', '%20', $res); 107 $res = preg_replace('/\*/', '%2A', $res); 108 $res = preg_replace('/%7E/', '~', $res); 109 return $res; 110 } 111 private function signString($source, $accessSecret){ 112 return base64_encode(hash_hmac('sha1', $source, $accessSecret, true)); 113 } 114}