https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1591472927710&di=fdd3da90a98ed401c87ddaf96fdbade2&imgtype=0&src=http%3A%2F%2F5b098... ...
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