日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

最新阿里云短信服务接口类【亲测成功】

發(fā)布時間:2024/3/13 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 最新阿里云短信服务接口类【亲测成功】 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

阿里大于

最新的阿里云短信接口,適用于阿里大于搬家以后的情況。
之前一直用阿里大于的短信接口,最近上項目時發(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;

????}

?

?

總結

以上是生活随笔為你收集整理的最新阿里云短信服务接口类【亲测成功】的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。