All Products
Search
Document Center

Short Message Service:Sample PHP code

Last Updated:Oct 10, 2022

This topic describes how to use the PHP SDK to call the SendMessageToGlobe operation to send SMS messages to regions outside the Chinese mainland.

Precautions

  • You must set the regionId parameter to ap-southeast-1.

  • You must set the product parameter to Dysmsapi.

  • You must set the host parameter to dysmsapi.ap-southeast-1.aliyuncs.com.

  • You must set the version parameter to 2018-05-01.

Sample code

<?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('SendMessageToGlobe')
                          ->method('POST')
                          ->options([
                                        'query' => [
                                            "To" => "62123****8901",
                                            // "From" => "1234567890",
                                            "Message" => "have a test.",
                                        ],
                                    ])
                          ->request();
    print_r($result->toArray());
} catch (ClientException $e) {
    echo $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {
    echo $e->getErrorMessage() . PHP_EOL;
}