老司机午夜精品视频播放-老司机午夜精品视频观看-老司机午夜精品视频在线观看免费-老司机午夜精品网站在线观看-亚洲综合精品成人-亚洲综合精品成人啪啪

您所在的位置:主頁 > 短信動(dòng)態(tài) >

阿里云短信接口調(diào)用(阿里云短信接口免費(fèi)發(fā)送多少條)

2021-11-17 19:03

使用阿里云短信API,需要在控制臺(tái)獲取以下必要參數(shù),其中需要自己手機(jī)驗(yàn)證+官方審核多次,尤其審核需要保持耐心。[阿里云短信接口調(diào)用(阿里云短信接口免費(fèi)發(fā)送多少條)]。

1. accessKeyId 相當(dāng)于你的個(gè)人賬戶密鑰;

2. accessKeySecret 與上是成對(duì)的;

3. SignName 個(gè)人簽名,在發(fā)出去的短信中,這個(gè)簽名會(huì)顯示在開頭,類似 【簽名】親愛的用戶...... 這種格式,SignName需要通過提交審核;

4.TemplateCode 模板代碼,阿里云短信是無法完全自定義短信的,需要通過審核的模板,然后自己再替換掉模板中的變量,如模板:“您的驗(yàn)證碼是${code}” ,code就是變量,使用時(shí)需設(shè)置變量值{"code":"12345"}(設(shè)置變量值的過程在demo中實(shí)現(xiàn)),短信發(fā)出去后變成:“您的驗(yàn)證碼是12345”,每個(gè)通過審核的模板會(huì)提供一個(gè)模板代碼;

最新的阿里云短信接口,適用于阿里大于搬家以后的情況。

之前一直用阿里大于的短信接口,最近上項(xiàng)目時(shí)發(fā)現(xiàn)阿里大于悄悄地搬家到了阿里云!阿里云的SDK文件繁多,看得一頭霧水!下面代碼是最新的可適用于阿里云短信服務(wù)的類,親測(cè)成功!

<?php
/**
 * 阿里云短信驗(yàn)證碼發(fā)送類
 * @author Administrator
 *
 */
class Sms {
 // 保存錯(cuò)誤信息
 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"}' //更換為自己的實(shí)際模版
 );
 //var_dump($params);die;
 // 計(jì)算簽名并把簽名結(jié)果加入請(qǐng)求參數(shù)
 $params ['Signature'] = $this->computeSignature ( $params, $this->accessKeySecret );
 // 發(fā)送請(qǐng)求(此處作了修改)
 //$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;
 }
 /**
 * 獲取詳細(xì)錯(cuò)誤信息
 *
 * @param unknown $status 
 */
 public function getErrorMessage($status) {
 // 阿里云的短信 亂八七糟的(其實(shí)是用的阿里大于)
 // 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' => '目標(biāo)手機(jī)號(hào)不正確,單次發(fā)送數(shù)量不能超過100',
 'InvalidParamString.MalFormed' => '短信模板中變量不是json格式',
 'InvalidParamStringTemplate.Malformed' => '短信模板中變量與模板內(nèi)容不匹配',
 'InvalidSendSms' => '觸發(fā)業(yè)務(wù)流控',
 'InvalidDayu.Malformed' => '變量不能是url,可以將變量固化在模板中'
 );
 if (isset ( $message [$status] )) {
 return $message [$status];
 }
 return $status;
 }
}

調(diào)用方法:

//生成驗(yàn)證碼
$mobile = 'xxxxxxx';
$code = rand ( 1000, 9999 );
//發(fā)送短信
$sms = new Sms();
//測(cè)試模式
$status = $sms->send_verify($mobile, $code);
if (!$status) {
 echo $sms->error;
}



圖片展示