1.登錄阿里云控制臺選擇免費開通短信服務(百度阿里云)
2.申請簽名和模板(根據要求操作)
(1)簽名就是我們收到的驗證碼短信開頭開頭【】中的內容
(2)模板就是文本內容

注意模板CODE,介面中會用到
(3)滑鼠移到右上角頭像,看到AccessKey管理,申請一個,介面中使用


(4)安裝SDK
composer require alibabacloud/sd
(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;
}
}
}
這些內容根據實際情況修改
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/27443.html
標籤:PHP
