This topic describes how to use the Alibaba Cloud SDK for PHP to call the SendMessageWithTemplate operation. This API operation allows you to send text messages to Mainland China.
Request parameters
Parameter | Type | Required | Example | Description |
---|---|---|---|---|
To | String | Yes | 45***0121 | The mobile phone number to receive the text message. Note that you must add the country code to the beginning of the mobile phone number. |
From | String | No | XXCompany | The ID of the sender. This parameter specifies the text message signature when you send the text message to Mainland China. |
TemplateCode | String | Yes | SMS_0000 | The ID of the text message template. You can view the ID of the text message template in the console of Short Message Service (SMS). |
TemplateParam | String | No | {"code":"1234","product":"ytx"} | The values of the variables that are included in the text message template. The values must use the JSON format. If line feeds are required in JSON-formatted data, the line feeds must meet the relevant requirements that are specified in the standard JSON protocol. |
SmsUpExtendCode | String | No | 90999 | The extension code of the upstream text message. This parameter is optional. You can skip this parameter based on your business requirements. |
Response parameters
Parameter | Type | Example | Description |
---|---|---|---|
To | String | 45***0121 | The mobile phone number that received the text message. Note that the country code is added to the beginning of the mobile phone number. |
MessageId | String | 10080***003003 | The ID of the text message. You can call the QueryMessage operation to query the delivery status of the text message based on the ID of the text message. |
ResponseCode | String | OK | The status code of the request. If the API operation returns OK, the request is successful. For more information about the other error codes, see Error codes. |
ResponseDescription | String | The text message is received. | The description of the status code. |
Segments | String | 1 | The number of the text messages that incurred fees. |
Install the core library of the Alibaba Cloud SDK for PHP
Use the Composer dependency manager. To download the core library of the Alibaba Cloud SDK for PHP, use the following link: Alibaba Cloud Client for PHP.
Sample requests
The considerations are described as follows:
- You must use the default value of the regionId parameter: ap-southeast-1.
- You must use the default value of the product parameter: Dysmsapi.
- You must use the default value of the host parameter: dysmsapi.ap-southeast-1.aliyuncs.com.
- You must use the default value of the version parameter: 2018-05-01.
<? php
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
// The download URL is https://github.com/aliyun/openapi-sdk-php-client.
// For information about the usage guide, visit the following page: 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;
}