tp5阿里雲短信驗證碼


1.登錄阿里雲賬號之后 首先鼠標移動到你的頭像上 點擊AccessKey之后 創建

2.找到短信服務這里 申請簽名 和 模板

3.之后找到 幫助文檔 -> 下載php SDK

 我是點擊的開發指南(舊版)下載的 php SDK  非輕量版的

下載解壓之后extend/alisms 

之后在app\common.php下

貼入代碼

<?php
use Aliyun\Core\Config;
use Aliyun\Core\Profile\DefaultProfile;
use Aliyun\Core\DefaultAcsClient;
use Aliyun\Api\Sms\Request\V20170525\SendSmsRequest;
/**
 * 短信發送
 * @param $to    接收人
 * @param $code   短信驗證碼
 * @return json
 */
function send_sms($to, $code){
    require_once '../extend/alisms/api_sdk/vendor/autoload.php';
    Config::load(); //加載區域結點配置
    $accessKeyId = '第一步創建的accessKey';
    $accessKeySecret = '第一步創建的accessKeySecret';
    $templateParam = $code;
    //短信API產品名(短信產品名固定,無需修改)
    $product = "Dysmsapi";
    //短信API產品域名(接口地址固定,無需修改)
    $domain = "dysmsapi.aliyuncs.com";
    //暫時不支持多Region(目前僅支持cn-hangzhou請勿修改)
    $region = "cn-hangzhou";
    // 初始化用戶Profile實例
    $profile = DefaultProfile::getProfile($region, $accessKeyId, $accessKeySecret);
    // 增加服務結點
    DefaultProfile::addEndpoint("cn-hangzhou", "cn-hangzhou", $product, $domain);
    // 初始化AcsClient用於發起請求
    $acsClient= new DefaultAcsClient($profile);
    // 初始化SendSmsRequest實例用於設置發送短信的參數
    $request = new SendSmsRequest();
    // 必填,設置雉短信接收號碼
    $request->setPhoneNumbers($to);
    // 必填,設置簽名名稱
    $request->setSignName("你的簽名名稱");
    // 必填,設置模板CODE
    $request->setTemplateCode("你的模板code");
    // 可選,設置模板參數
    if($templateParam) {
        $request->setTemplateParam(json_encode(['code'=>$templateParam]));//由於我的模板里需要傳遞我的短信驗證碼
    }
    //發起訪問請求
    $acsResponse = $acsClient->getAcsResponse($request);
    //返回請求結果
    $result = json_decode(json_encode($acsResponse),true);
    // 具體返回值參考文檔:https://help.aliyun.com/document_detail/55451.html?spm=a2c4g.11186623.6.563.YSe8FK
    return $result;
}
?>
<?php
public function code(){
        $code = rand(100000,999999);
        $phone = "填寫你的手機號";
        $result = send_sms($phone,$code);
        var_dump($result);
    }

?> 

之后測試 就可以了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM