Integrating Alibaba Cloud SMS Service with ThinkPHP6
This guide explains how to set up Alibaba Cloud SMS, configure the required Access Key ID and Secret, install the SDK, and use ThinkPHP6 to call the SMS API with sample PHP code, highlighting the steps and placeholders that must be replaced with your own credentials.
Alibaba Cloud SMS is a fast, efficient, cost‑effective messaging solution provided by Alibaba Cloud, supporting global SMS delivery for scenarios such as registration, login, and verification codes.
To integrate the service with ThinkPHP6, follow these steps: (1) enable the SMS service on Alibaba Cloud and obtain the Access Key ID and Access Key Secret; (2) install the Alibaba Cloud SDK via Composer; (3) configure the SDK in your project with the obtained keys; (4) invoke the SDK’s SMS API to send messages; (5) optionally handle the SDK’s response.
The following PHP code demonstrates the integration, with comments explaining each part. Replace the highlighted placeholders (Access Key ID, Access Key Secret, phone number, sign name, and template code) with your own values.
<code>// 引入阿里云SDK</code><code>use AlibabaCloud\Client\AlibabaCloud;</code><code>use AlibabaCloud\Client\Exception\ClientException;</code><code>use AlibabaCloud\Client\Exception\ServerException;</code><code>// 配置阿里云SDK</code><code>AlibabaCloud::accessKeyClient('Access Key ID', 'Access Key Secret')</code><code> ->regionId('cn-hangzhou')</code><code> ->asDefaultClient();</code><code>// 发送短信</code><code>try {</code><code> $result = AlibabaCloud::rpc()</code><code> ->product('Dysmsapi')</code><code> // 指定短信API名称</code><code> ->version('2017-05-25')</code><code> // 指定API的版本号</code><code> ->action('SendSms')</code><code> // 指定API的Action</code><code> ->method('POST')</code><code> // 指定HTTP请求方法</code><code> ->host('dysmsapi.aliyuncs.com')</code><code> ->options(['query' => [</code><code> 'RegionId' => "cn-hangzhou",
'PhoneNumbers' => "手机号码",
// 目标手机号码,多个手机号码可以逗号分隔
'SignName' => "签名名称",
// 阿里云短信服务签名名称
'TemplateCode' => "模板CODE",
// 阿里云短信服务模板CODE
'TemplateParam' => '{"code":"1234"}',
// 阿里云短信服务模板变量
]])</code><code> ->request();</code><code> print_r($result->toArray());</code><code> // 输出阿里云SDK返回的短信发送结果</code><code>} catch (ClientException $exception) {</code><code> echo $exception->getMessage();</code><code>} catch (ServerException $exception) {</code><code> echo $exception->getMessage();</code><code>}</code>php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.