阿里云PHP SMS短信服務(wù)驗(yàn)證碼發(fā)送方法
開通SMS服務(wù)
首先去這個(gè)網(wǎng)站開通阿里云的SMS短信服務(wù):https://www.aliyun.com/product/sms?spm=5176.8142029.388261.295.vU5T5g
創(chuàng)建簽名、模板
要使用短信服務(wù)器需要先創(chuàng)建簽名和模板,并提交給阿里云審核通過才可以正常使用短信服務(wù)。

創(chuàng)建簽名
創(chuàng)建簽名的時(shí)候注意一下簽名名稱,其他的話就不累贅了。

記住簽名名稱
現(xiàn)在請(qǐng)記住你創(chuàng)建的簽名名稱,一會(huì)在代碼中需要使用。
創(chuàng)建模板
創(chuàng)建模板也很簡單,阿里云已經(jīng)把要如何填寫寫的很清楚了。

查看并記住模板CODE
返回你的控制臺(tái),當(dāng)你的模板審核通過時(shí)這就會(huì)出現(xiàn)大于0的數(shù)。
點(diǎn)擊這個(gè)數(shù),會(huì)進(jìn)入模板管理面板就能看到你的模板CODE了,請(qǐng)記住他。


創(chuàng)建并記住KeyId和KeySecret
到控制臺(tái),把鼠標(biāo)放到右上角你的用戶名的位置會(huì)出現(xiàn)一個(gè)accessKeySecret點(diǎn)進(jìn)去就可以創(chuàng)建KeyId和KeySecret了,如果他提醒你用RAM安全什么的,你看你要不要給你的員工分配權(quán)限,如果要的話就用RAM,否則就直接點(diǎn)擊繼續(xù)使用就行了。


下載阿里云短信服務(wù)器PHP-SDK
官方下載地址:https://help.aliyun.com/document_detail/55359.html?spm=5176.8195934.507901.12.b1ngGK
本教程使用SDK下載地址:http://pan.baidu.com/s/1bpF5B8z
密匙:pult

創(chuàng)建PHP-SMS項(xiàng)目
創(chuàng)建代碼文件
創(chuàng)建你的代碼文件,并把這個(gè)文件放在剛才下載的SDK文件夾中的api_sdk的aliyun-php-sdk-core目錄下,并把一下代碼寫入代碼文件。
aliyun-php-sdk-core目錄里包含了SMS短信服務(wù)的各種模塊,所以必須得放在這里面才能使用服務(wù)
<?php
include 'Config.php';
include_once 'Request/V20170525/SendSmsRequest.php';
include_once 'Request/V20170525/QuerySendDetailsRequest.php';
$accessKeyId = "LTAIvAaNs61JeBiN";
//阿里云KeyId
$accessKeySecret = "Y3H7durYJ6GIqmJJrsdbJwPi6E8O8M";
//阿里云KeySecret
//短信API產(chǎn)品名
$product = "Dysmsapi";
//照寫就行了
//短信API產(chǎn)品域名
$domain = "dysmsapi.aliyuncs.com";
//照著寫就行了
//暫時(shí)不支持多Region
$region = "cn-hangzhou";
//照著寫就行了
//初始化訪問的acsCleint
$profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);
DefaultProfile::addEndpoint("cn-hangzhou", "cn-hangzhou", $product, $domain);
$acsClient= new DefaultAcsClient($profile);
$request = new SendSmsRequest;
//必填-短信接收號(hào)碼。支持以逗號(hào)分隔的形式進(jìn)行批量調(diào)用,批量上限為20個(gè)手機(jī)號(hào)碼,批量調(diào)用相對(duì)于單條調(diào)用及時(shí)性稍有延遲,驗(yàn)證碼類型的短信推薦使用單條調(diào)用的方式
$request->setPhoneNumbers("123456789");
//這里填你要發(fā)送的電話號(hào)碼
//必填-短信簽名
$request->setSignName("xx項(xiàng)目");
//這里就是剛才讓你記住的項(xiàng)目簽名
//必填-短信模板Code
$request->setTemplateCode("SMS_123456");
//這里就是模板CODE
//選填-假如模板中存在變量需要替換則為必填(JSON格式)
$request->setTemplateParam("{\"name\":\"郭濤\",\"number\":\"316\"}");
//選填-發(fā)送短信流水號(hào)
$request->setOutId("1234");//照填就行了
//發(fā)起訪問請(qǐng)求
$acsResponse = $acsClient->getAcsResponse($request);
var_dump($acsResponse);//返回結(jié)果
移入Requset
還是在下載的SDK文件夾中的api_sdk目錄下,有一個(gè)交Dysmsapi的文件夾,打開這個(gè)文件夾就會(huì)看到一個(gè)叫Request的文件夾,把這個(gè)Reques。的件夾復(fù)制粘貼到aliyun-php-sdk-core里面。說實(shí)在的我搞不清阿里云這個(gè)為什么要這樣分開裝SDK,可能是我使用的姿勢(shì)不對(duì)吧,如果有大神搞得清,還勞煩賜教小弟,好人一生平安。
移入后,打開Request\V20170525目錄里有一個(gè)SendSmsRequest.php的源文件。請(qǐng)屏蔽第一行的空間命名。 也就是這一行namespace Dysmsapi\Reqest\V20170525;最后效果如下
<?php
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
//namespace Dysmsapi\Request\V20170525;//就是屏蔽這一行代碼!?。?!
class SendSmsRequest extends \RpcAcsRequest
{
function __construct()
{
parent::__construct("Dysmsapi", "2017-05-25", "SendSms");
}
private $outId;
private $signName;
private $ownerId;
private $resourceOwnerId;
private $templateCode;
private $phoneNumbers;
private $resourceOwnerAccount;
private $templateParam;
public function getOutId() {
return $this->outId;
}
public function setOutId($outId) {
$this->outId = $outId;
$this->queryParameters["OutId"]=$outId;
}
public function getSignName() {
return $this->signName;
}
public function setSignName($signName) {
$this->signName = $signName;
$this->queryParameters["SignName"]=$signName;
}
public function getOwnerId() {
return $this->ownerId;
}
public function setOwnerId($ownerId) {
$this->ownerId = $ownerId;
$this->queryParameters["OwnerId"]=$ownerId;
}
public function getResourceOwnerId() {
return $this->resourceOwnerId;
}
public function setResourceOwnerId($resourceOwnerId) {
$this->resourceOwnerId = $resourceOwnerId;
$this->queryParameters["ResourceOwnerId"]=$resourceOwnerId;
}
public function getTemplateCode() {
return $this->templateCode;
}
public function setTemplateCode($templateCode) {
$this->templateCode = $templateCode;
$this->queryParameters["TemplateCode"]=$templateCode;
}
public function getPhoneNumbers() {
return $this->phoneNumbers;
}
public function setPhoneNumbers($phoneNumbers) {
$this->phoneNumbers = $phoneNumbers;
$this->queryParameters["PhoneNumbers"]=$phoneNumbers;
}
public function getResourceOwnerAccount() {
return $this->resourceOwnerAccount;
}
public function setResourceOwnerAccount($resourceOwnerAccount) {
$this->resourceOwnerAccount = $resourceOwnerAccount;
$this->queryParameters["ResourceOwnerAccount"]=$resourceOwnerAccount;
}
public function getTemplateParam() {
return $this->templateParam;
}
public function setTemplateParam($templateParam) {
$this->templateParam = $templateParam;
$this->queryParameters["TemplateParam"]=$templateParam;
}
}
完成
運(yùn)行試試吧

以上所述是小編給大家介紹的阿里云PHP SMS短信服務(wù)驗(yàn)證碼發(fā)送方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- 阿里云服務(wù)器ping不通解決辦法(云服務(wù)器搭建完環(huán)境訪問不了ip解決辦法)
- 阿里云LNMP 云服務(wù)器重啟后網(wǎng)站打不開解決方法
- 阿里云linux服務(wù)器下安裝Apache的簡單方法
- 阿里云主機(jī)Windows 2008服務(wù)器硬盤分區(qū)和格式化圖文教程
- 阿里云云服務(wù)器Windows 2008下IIS添加網(wǎng)站綁定域名圖文教程
- 阿里云云服務(wù)器Windows 2008中的FTP配置圖文教程
- 阿里云服務(wù)器CentOS服務(wù)器初始化設(shè)置步驟
- Linux使用scp遠(yuǎn)程傳輸命令進(jìn)行服務(wù)器文件互傳(阿里云服務(wù)器)
- 阿里云centos7服務(wù)器搭建nginx web服務(wù)經(jīng)驗(yàn)示例
- 搭建阿里云ecs服務(wù)器之安裝圖形化界面的方法
- 阿里云服務(wù)器實(shí)現(xiàn)域名解析步驟(小白教程)
- .net core 使用阿里云分布式日志的配置方法
- 阿里云日志服務(wù)日志過濾器配置
相關(guān)文章
phpstorm安裝xdebug(phpstudy環(huán)境下)成功運(yùn)行的操作步驟
這篇文章主要介紹了phpstorm安裝xdebug(phpstudy環(huán)境下)成功運(yùn)行,本文分步驟給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09
php實(shí)現(xiàn)的支付寶網(wǎng)頁支付功能示例【基于TP5框架】
這篇文章主要介紹了php實(shí)現(xiàn)的支付寶網(wǎng)頁支付功能,結(jié)合實(shí)例形式分析了基于TP5框架框架的支付寶網(wǎng)頁支付功能具體操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-09-09
ThinkPHP入庫出現(xiàn)兩次反斜線轉(zhuǎn)義及數(shù)據(jù)庫類轉(zhuǎn)義的解決方法
這篇文章主要介紹了ThinkPHP入庫出現(xiàn)兩次反斜線轉(zhuǎn)義及數(shù)據(jù)庫類轉(zhuǎn)義的解決方法,主要通過針對(duì)magic_quotes_gpc開啟的情況下進(jìn)行檢查與判斷轉(zhuǎn)義來實(shí)現(xiàn),需要的朋友可以參考下2014-11-11
簡單易用的php數(shù)據(jù)庫pdo操作類(curd?demo)
這篇文章主要介紹了簡單易用的php數(shù)據(jù)庫pdo操作類(curd?demo),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
Codeigniter框架實(shí)現(xiàn)獲取分頁數(shù)據(jù)和總條數(shù)的方法
這篇文章主要介紹了Codeigniter框架實(shí)現(xiàn)獲取分頁數(shù)據(jù)和總條數(shù)的方法,實(shí)現(xiàn)了對(duì)獲取當(dāng)前頁的數(shù)據(jù)和總條數(shù)方法的封裝,是非常實(shí)用的技巧,需要的朋友可以參考下2014-12-12
win平臺(tái)安裝配置Nginx+php+mysql 環(huán)境
本文給大家分享的是win平臺(tái)安裝配置Nginx+php+mysql 環(huán)境的方法和步驟,有需要的小伙伴可以參考下。2016-01-01
基于laravel-admin 后臺(tái) 列表標(biāo)簽背景的使用方法
今天小編就為大家分享一篇基于laravel-admin 后臺(tái) 列表標(biāo)簽背景的使用方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-10-10

