萬商超信
阿里云短信接口調(diào)用(阿里云短信接口免費發(fā)送多少條)
2021-11-17 19:03
使用阿里云短信API,需要在控制臺獲取以下必要參數(shù),其中需要自己手機驗證+官方審核多次,尤其審核需要保持耐心。[阿里云短信接口調(diào)用(阿里云短信接口免費發(fā)送多少條)]。
1. accessKeyId 相當于你的個人賬戶密鑰;
2. accessKeySecret 與上是成對的;
3. SignName 個人簽名,在發(fā)出去的短信中,這個簽名會顯示在開頭,類似 【簽名】親愛的用戶...... 這種格式,SignName需要通過提交審核;
4.TemplateCode 模板代碼,阿里云短信是無法完全自定義短信的,需要通過審核的模板,然后自己再替換掉模板中的變量,如模板:“您的驗證碼是${code}” ,code就是變量,使用時需設置變量值{"code":"12345"}(設置變量值的過程在demo中實現(xiàn)),短信發(fā)出去后變成:“您的驗證碼是12345”,每個通過審核的模板會提供一個模板代碼;
最新的阿里云短信接口,適用于阿里大于搬家以后的情況。
之前一直用阿里大于的短信接口,最近上項目時發(fā)現(xiàn)阿里大于悄悄地搬家到了阿里云!阿里云的SDK文件繁多,看得一頭霧水!下面代碼是最新的可適用于阿里云短信服務的類,親測成功!
<?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-dTH:i:sZ' ), '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' => '短信模板中變量與模板內(nèi)容不匹配', 'InvalidSendSms' => '觸發(fā)業(yè)務流控', 'InvalidDayu.Malformed' => '變量不能是url,可以將變量固化在模板中' ); if (isset ( $message [$status] )) { return $message [$status]; } return $status; } }
調(diào)用方法:
//生成驗證碼 $mobile = 'xxxxxxx'; $code = rand ( 1000, 9999 ); //發(fā)送短信 $sms = new Sms(); //測試模式 $status = $sms->send_verify($mobile, $code); if (!$status) { echo $sms->error; }