新版阿里雲短信服務之發送驗證碼接口快速實現

1.登陸阿里雲控制檯選擇免費開通短信服務(百度阿里雲)php

2.申請簽名和模板(根據要求操做)html

(1)簽名就是咱們收到的驗證碼短信開頭開頭【】中的內容git

(2)模板就是文本內容github

注意模板CODE,接口中會用到json

(3)鼠標移到右上角頭像,看到AccessKey管理,申請一個,接口中使用api

(4)安裝SDKcomposer

composer require alibabacloud/sdui

(5)發送驗證碼的API接口
簡單封裝一個類,調用便可阿里雲

<?php
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

// Download:https://github.com/aliyun/openapi-sdk-php
// Usage:https://github.com/aliyun/openapi-sdk-php/blob/master/README.md

class Sms
{
   public function sendSms($phoneNum, $code)
   {
      AlibabaCloud::accessKeyClient('你的<accessKeyId>', '你的<accessSecret>')
                        ->regionId('cn-hangzhou')
                        ->asDefaultClient();

      try {
          $result = AlibabaCloud::rpc()
                          ->product('Dysmsapi')
                          // ->scheme('https') // https | http
                          ->version('2017-05-25')
                          ->action('SendSms')
                          ->method('POST')
                          ->host('dysmsapi.aliyuncs.com')
                          ->options([
                                        'query' => [
                                          'RegionId' => "cn-hangzhou",
                                          'PhoneNumbers' => $phoneNum,
                                          'SignName' => "申請的簽名",
                                          'TemplateCode' => "申請的模板CODE",
                                          'TemplateParam' => $code(json格式),
                                        ],
                                    ])
                          ->request();
          print_r($result->toArray());
      } catch (ClientException $e) {
          echo $e->getErrorMessage() . PHP_EOL;
      } catch (ServerException $e) {
          echo $e->getErrorMessage() . PHP_EOL;
      }
   } 
}

這些內容根據實際狀況修改code

AlibabaCloud::accessKeyClient('你的<accessKeyId>', '你的<accessSecret>')

                                          'PhoneNumbers' => $phoneNum,
                                          'SignName' => "申請的簽名",
                                          'TemplateCode' => "申請的模板CODE",
                                          'TemplateParam' => $code(json格式),

以上即是短信驗證碼的簡單發送接口
更多內容,見阿里雲短信服務文檔https://help.aliyun.com/document_detail/101414.html?spm=a2c4g.11186623.4.4.145650a4HUKi5u

相關文章
相關標籤/搜索