原創(chuàng)文章來源:墨白`Blog
一、config.php配置文件配置公共參數(shù)(代碼如下):
//阿里大魚
? ? 'Ali_SMS' =>array(
? ? ? ? 'sms_temp' =>'短信模板',
? ? ? ? 'sms_sign' =>'簽名',
? ? ? ? 'appkey'? =>'appkey',
? ? ? ? 'secretKey'=>'secretKey',
? ? ),
二、創(chuàng)建測試控制器TestController.php(代碼如下):
Vendor('alisms.Alisms');
? ? ? ? $alisms=new\Alisms(C('Ali_SMS.appkey'),C('Ali_SMS.secretKey'));
? ? ? ? $mobile=$phone;
? ? ? ? $temp_code= C('Ali_SMS.sms_temp');
? ? ? ? $paramString= '{"code":"'.$code.'"}';
? ? ? ? $alisms->signName = C('Ali_SMS.sms_sign');
? ? ? ? $re=$alisms->smsend($mobile,$temp_code,$paramString);
? ? ? ? if($re['Code'] =='OK'){
? ? ? ? ? ? $info['status'] = 1;
? ? ? ? ? ? $info['info']? = '短信發(fā)送成功!';
? ? ? ? ? ? echojson_encode($info);
? ? ? ? ? ? exit;
? ? ? ? }else{
? ? ? ? ? ? $info['info']? = '短信發(fā)送失敗';
? ? ? ? ? ? $info['status'] = 0;
? ? ? ? ? ? echojson_encode($info);
? ? ? ? ? ? exit;
? ? ? ? }
三、創(chuàng)建阿里接口使用Alisms.php類(代碼如下):
<?php
/**
* 阿里云短信接口
* @author 墨白<453885726@qq.com>
* 示例
*? ? $alisms = new \Common\Model\Alisms($accessKeyId,$accessKeySecret);
*? ? ? $mobile = '18788830181';
*? ? ? $code? = 'SMS_36225243';
*? ? ? $paramString = '{"code":"344556"}';
*? ? ? $re = $alisms->smsend($mobile,$code,$paramString);
*? ? ? print_r($re);
*
*/
class Alisms{
? ? public $config = array(
? ? ? ? ? ? ? 'Format'? =>'json', //返回值的類型,支持JSON與XML。默認(rèn)為XML
? ? ? ? ? ? ? 'Version' =>'2017-05-25', //API版本號,為日期形式:YYYY-MM-DD,本版本對應(yīng)為2016-09-27
? ? ? ? ? ? ? 'SignatureMethod' =>'HMAC-SHA1', //簽名方式,目前支持HMAC-SHA1
? ? ? ? ? ? ? 'SignatureVersion'=>'1.0',
? ? ? ? ? ? );
? ? private? ? $accessKeySecret;? ?
? ? private? ? $http = 'http://dysmsapi.aliyuncs.com';//https://sms.aliyuncs.com/';? ? ? ? //短信接口
? ? private? ? $dateTimeFormat = 'Y-m-d\TH:i:s\Z';
? ? public? ? $signName = '短信簽名'; //管理控制臺中配置的短信簽名(狀態(tài)必須是驗(yàn)證通過)
? ? public? ? $method = 'GET';
? ? /**
? ? *發(fā)送短信
? ? *@AccessKeyId? ? ? 阿里云申請的 Access Key ID
? ? *@AccessKeySecret? 阿里云申請的 Access Key Secret
? ? */
? ? function __construct($accessKeyId,$accessKeySecret){
? ? ? ? $this->config['AccessKeyId'] = $accessKeyId;
? ? ? ? $this->AccessKeySecret = $accessKeySecret;
? ? }
? ? /**
? ? *發(fā)送短信
? ? *@mobile? 目標(biāo)手機(jī)號,多個手機(jī)號可以逗號分隔
? ? *@code 短信模板的模板CODE
? ? *@ParamString? 短信模板中的變量;,參數(shù)格式{“no”:”123456”}, 個人用戶每個變量長度必須小于15個字符
? ? */
? ? public function smsend($mobile,$code,$ParamString){
? ? ? ? $apiParams = $this->config;
? ? ? ? $apiParams["Action"]? ? ? ? = 'SendSms';//'SingleSendSms';
? ? ? ? $apiParams['TemplateCode']? ? = $code;? //短信模板的模板CODE
? ? ? ? //$apiParams['RecNum']? ? ? ? = $mobile;? //目標(biāo)手機(jī)號,多個手機(jī)號可以逗號分隔
? ? ? ? //$apiParams['ParamString']? ? = $ParamString;? //短信模板中的變量;,此參數(shù)傳遞{“no”:”123456”}, 個人用戶每個變量長度必須小于15個字符
? ? ? ? $apiParams['SignName']? ? ? ? = $this->signName;? //管理控制臺中配置的短信簽名(狀態(tài)必須是驗(yàn)證通過)
? ? ? ? date_default_timezone_set("GMT");
? ? ? ? $apiParams["Timestamp"] = date($this->dateTimeFormat);
? ? ? ? $apiParams["SignatureNonce"]? = md5(md5('wbh').rand(100000,999999).uniqid()); //唯一隨機(jī)數(shù)
? ? ? ? $apiParams['RegionId'] = 'cn-hangzhou';
? ? ? ? $apiParams['PhoneNumbers'] = $mobile;
? ? ? ? $apiParams['TemplateParam'] = $ParamString;
? ? ? ? $apiParams["Signature"] = $this->computeSignature($apiParams, $this->AccessKeySecret);//簽名
? ? ? ? $tag = '?';
? ? ? ? $requestUrl = $this->http;
? ? ? ? foreach ($apiParams as $apiParamKey => $apiParamValue){
? ? ? ? ? ? $requestUrl .= $tag."$apiParamKey=" . urlencode($apiParamValue);
? ? ? ? ? ? $tag = '&';
? ? ? ? }
? ? ? ? return $this->postSMS($requestUrl);
? ? }
? ? private function postSMS($url){
? ? ? ? $ch = curl_init();
? ? ? ? curl_setopt($ch, CURLOPT_URL, $url);
? ? ? ? curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
? ? ? ? curl_setopt($ch, CURLOPT_HEADER, 0);
? ? ? ? $output = curl_exec($ch);
? ? ? ? curl_close($ch);
? ? ? ? return json_decode($output,true);
/*
? ? ? ? $opts = array(
? ? ? ? ? ? 'http'=>array(
? ? ? ? ? ? ? ? 'method'=>$this->method,
? ? ? ? ? ? ? ? 'timeout'=>600,
? ? ? ? ? ? ? ? 'header'=>'Content-Type: application/x-www-form-urlencoded',
? ? ? ? ? ? )
? ? ? ? );
? ? ? ? $html = file_get_contents($url, false, stream_context_create($opts));? ?
? ? ? ? if($html){
? ? ? ? ? ? return json_decode($html,true);
? ? ? ? }else{
? ? ? ? ? ? return false;
? ? ? ? }*/
? ? }
? ? //生成取短信簽名
? ? private function computeSignature($parameters, $accessKeySecret){
? ? ? ? ksort($parameters);
? ? ? ? $canonicalizedQueryString = '';
? ? ? ? foreach($parameters as $key => $value){
? ? ? ? ? ? $canonicalizedQueryString .= '&' . $this->percentEncode($key). '=' . $this->percentEncode($value);
? ? ? ? }? ?
? ? ? ? $stringToSign = $this->method.'&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 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;
? ? }
? ? private function signString($source, $accessSecret){
? ? ? ? return? ? base64_encode(hash_hmac('sha1', $source, $accessSecret, true));
? ? }
}