本文为您介绍了PHP调用SendMessageWithTemplate发送中国地区短信。
请求参数
名称 | 类型 | 是否必选 | 示例值 | 描述 |
---|---|---|---|---|
To | String | 是 | 45***0121 | 接收方的手机号码;说明:号码格式为国家码+号码。 |
From | String | 否 | XXCompany | 发送方标识;说明:发往中国时,该参数为短信签名。 |
TemplateCode | String | 是 | SMS_0000 | 短信模板ID。请在控制台中查看。 |
TemplateParam | String | 否 | {“code”:”1234”,”product”:”ytx”} | 短信模板变量对应的实际值,JSON格式;说明:如果JSON中需要带换行符,请参照标准的JSON协议处理。 |
SmsUpExtendCode | String | 选填 | 90999 | 上行短信扩展码,无特殊需要此字段的用户请忽略此字段。 |
返回数据
名称 | 类型 | 示例值 | 描述 |
---|---|---|---|
To | String | 45***0121 | 接收方的手机号码;说明:号码格式为国家码+号码。 |
MessageId | String | 10080***003003 | 短信发送ID,可根据该ID在接口QueryMessage中查询具体的发送状态。 |
ResponseCode | String | OK | 请求状态码,返回OK代表请求成功,其他错误码详见错误码列表。 |
ResponseDescription | String | 短信接收成功 | 状态码的描述。 |
Segments | String | 1 | 短信计费条数。 |
引入阿里云核心包
使用Composer安装,点击查看下载地址。
调用示例
注意事项如下:
- regionId必须为ap-southeast-1,请勿修改。
- product必须为Dysmsapi,请勿修改。
- host必须为dysmsapi.ap-southeast-1.aliyuncs.com,请勿修改。
- version必须为2018-05-01,请勿修改。
<?php
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
// Download:https://github.com/aliyun/openapi-sdk-php-client
// Usage:https://github.com/aliyun/openapi-sdk-php-client/blob/master/README-CN.md
AlibabaCloud::accessKeyClient('<accessKeyId>', '<accessSecret>')
->regionId('ap-southeast-1')
->asGlobalClient();
try {
$result = AlibabaCloud::rpcRequest()
->product('Dysmsapi')
->host('dysmsapi.ap-southeast-1.aliyuncs.com')
->version('2018-05-01')
->action('SendMessageWithTemplate')
->method('POST')
->options([
'query' => [
"To" => "8615200000000",
"From" => "SMS signature",
"TemplateCode" => "SMS_225001",
"TemplateParam" => "{\"code\":\"1234\"}",
"SmsUpExtendCode" => "12345",
],
])
->request();
print_r($result->toArray());
} catch (ClientException $e) {
echo $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {
echo $e->getErrorMessage() . PHP_EOL;
}