最新阿里云短信服务接口类【亲测成功】
阿里大于
最新的阿里云短信接口,適用于阿里大于搬家以后的情況。
之前一直用阿里大于的短信接口,最近上項目時發(fā)現(xiàn)阿里大于悄悄地搬家到了阿里云!阿里云的SDK文件繁多,看得一頭霧水!下面代碼是最新的可適用于阿里云短信服務的類,親測成功!
之前有大神放出過代碼,鏈接:http://www.thinkphp.cn/code/2543.html,但是經過測試會返回“ 短信接口返回錯誤碼:InvalidDayuStatus.Malformed ,賬戶短信開通狀態(tài)不正確”!(經過測試,該大神代碼適用于阿里大于接口,不適用于最新的阿里云控制臺內的短信服務)
經過詳細查閱SDK介紹,鏈接地址:https://help.aliyun.com/document_detail/55359.html?spm=5176.doc54229.6.564.5cPOVA中的一段
——————————————————————————
“您好,暫時沒有其他語言的SDK,我們正在努力更新中,敬請期待!sdk只是幫助拼接了http請求和解析返回報文,如若sdk版本問題無法編譯通過,可以 自行拼接下http請求來使用。接口本身只是一個http請求調用,沒有語言的限制。拼接http請求請參照上面的鏈接,也可以參照sdk中的 源碼:https://help.aliyun.com/document_detail/56189.html?spm=5176.product44282.6.562.3wM8lS”
對上面大神代碼進行改裝(主要修改傳入?yún)?shù)及網關)如下,成功!
<?php
/**
*?阿里云短信驗證碼發(fā)送類
*?@author?Administrator
*
*/
class?Sms?{
//?保存錯誤信息
public?$error;
//?Access?Key?ID
private?$accessKeyId?=?'';
//?Access?Access?Key?Secret
private?$accessKeySecret?=?'';
//?簽名
private?$signName?=?'';
//?模版ID
private?$templateCode?=?'';
public?function?__construct($cofig?=?array())?{
$cofig?=?array?(
'accessKeyId'?=>?'xxxxxxxxxxx',
'accessKeySecret'?=>?'xxxxxxxxxx',
'signName'?=>?'你的簽名',
'templateCode'?=>?'SMS_76510109'
);
//?配置參數(shù)
$this->accessKeyId?=?$cofig?['accessKeyId'];
$this->accessKeySecret?=?$cofig?['accessKeySecret'];
$this->signName?=?$cofig?['signName'];
$this->templateCode?=?$cofig?['templateCode'];
}
private?function?percentEncode($string)?{
$string?=?urlencode?(?$string?);
$string?=?preg_replace?(?'/\+/',?'%20',?$string?);
$string?=?preg_replace?(?'/\*/',?'%2A',?$string?);
$string?=?preg_replace?(?'/%7E/',?'~',?$string?);
return?$string;
}
/**
*?簽名
*
*?@param?unknown?$parameters
*?@param?unknown?$accessKeySecret
*?@return?string
*/
private?function?computeSignature($parameters,?$accessKeySecret)?{
ksort?(?$parameters?);
$canonicalizedQueryString?=?'';
foreach?(?$parameters?as?$key?=>?$value?)?{
$canonicalizedQueryString?.=?'&'?.?$this->percentEncode?(?$key?)?.?'='?.?$this->percentEncode?(?$value?);
}
$stringToSign?=?'GET&%2F&'?.?$this->percentencode?(?substr?(?$canonicalizedQueryString,?1?)?);
$signature?=?base64_encode?(?hash_hmac?(?'sha1',?$stringToSign,?$accessKeySecret?.?'&',?true?)?);
return?$signature;
}
/**
*?@param?unknown?$mobile
*?@param?unknown?$verify_code
*
*/
public?function?send_verify($mobile,?$verify_code)?{
$params?=?array?(???//此處作了修改
'SignName'?=>?$this->signName,
'Format'?=>?'JSON',
'Version'?=>?'2017-05-25',
'AccessKeyId'?=>?$this->accessKeyId,
'SignatureVersion'?=>?'1.0',
'SignatureMethod'?=>?'HMAC-SHA1',
'SignatureNonce'?=>?uniqid?(),
'Timestamp'?=>?gmdate?(?'Y-m-d\TH:i:s\Z'?),
'Action'?=>?'SendSms',
'TemplateCode'?=>?$this->templateCode,
'PhoneNumbers'?=>?$mobile,
//'TemplateParam'?=>?'{"code":"'?.?$verify_code?.?'"}'
'TemplateParam'?=>?'{"time":"1234"}'???//更換為自己的實際模版
);
//var_dump($params);die;
//?計算簽名并把簽名結果加入請求參數(shù)
$params?['Signature']?=?$this->computeSignature?(?$params,?$this->accessKeySecret?);
//?發(fā)送請求(此處作了修改)
//$url?=?'https://sms.aliyuncs.com/?'?.?http_build_query?(?$params?);
$url?=?'http://dysmsapi.aliyuncs.com/?'?.?http_build_query?(?$params?);
?
$ch?=?curl_init?();
curl_setopt?(?$ch,?CURLOPT_URL,?$url?);
curl_setopt?(?$ch,?CURLOPT_SSL_VERIFYPEER,?FALSE?);
curl_setopt?(?$ch,?CURLOPT_SSL_VERIFYHOST,?FALSE?);
curl_setopt?(?$ch,?CURLOPT_RETURNTRANSFER,?1?);
curl_setopt?(?$ch,?CURLOPT_TIMEOUT,?10?);
$result?=?curl_exec?(?$ch?);
curl_close?(?$ch?);
$result?=?json_decode?(?$result,?true?);
//var_dump($result);die;
if?(isset?(?$result?['Code']?))?{
$this->error?=?$this->getErrorMessage?(?$result?['Code']?);
return?false;
}
return?true;
}
/**
*?獲取詳細錯誤信息
*
*?@param?unknown?$status
*/
public?function?getErrorMessage($status)?{
//?阿里云的短信?亂八七糟的(其實是用的阿里大于)
//?https://api.alidayu.com/doc2/apiDetail?spm=a3142.7629140.1.19.SmdYoA&apiId=25450
$message?=?array?(
'InvalidDayuStatus.Malformed'?=>?'賬戶短信開通狀態(tài)不正確',
'InvalidSignName.Malformed'?=>?'短信簽名不正確或簽名狀態(tài)不正確',
'InvalidTemplateCode.MalFormed'?=>?'短信模板Code不正確或者模板狀態(tài)不正確',
'InvalidRecNum.Malformed'?=>?'目標手機號不正確,單次發(fā)送數(shù)量不能超過100',
'InvalidParamString.MalFormed'?=>?'短信模板中變量不是json格式',
'InvalidParamStringTemplate.Malformed'?=>?'短信模板中變量與模板內容不匹配',
'InvalidSendSms'?=>?'觸發(fā)業(yè)務流控',
'InvalidDayu.Malformed'?=>?'變量不能是url,可以將變量固化在模板中'
);
if?(isset?(?$message?[$status]?))?{
return?$message?[$status];
}
return?$status;
}
}
?
調用代碼如下:
//生成驗證碼
????$mobile?=?'xxxxxxx';
????$code?=?rand?(?1000,?9999?);
????//發(fā)送短信
????$sms?=?new?Sms();
????????
????//測試模式
????$status?=?$sms->send_verify($mobile,?$code);
????????
????if?(!$status)?{
????????echo?$sms->error;
????}
?
?
總結
以上是生活随笔為你收集整理的最新阿里云短信服务接口类【亲测成功】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 招聘:JAVA软件开发工程师
- 下一篇: 日记侠:你对微信关键词是如何理解的?