TP3.2.3 接入阿里sms 短信接口

原創(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));

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容