Configure an endpoint
An endpoint is a domain name of an Alibaba Cloud service API. You can configure an endpoint by using SDK for PHP V1.0 in multiple ways.
SDK for PHP V1.0 provides the following addressing modes for endpoints, listed in descending order of priority.
-
User-defined addressing mode. This mode has the highest priority. You can specify an exact endpoint.
<?php require_once 'vendor/autoload.php'; use AlibabaCloud\Client\AlibabaCloud; use AlibabaCloud\Client\Exception\ClientException; use AlibabaCloud\Client\Exception\ServerException; use AlibabaCloud\Ecs\Ecs; try { // Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured. AlibabaCloud::accessKeyClient(getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'), getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET')); // Specify an endpoint of Elastic Compute Service (ECS) in a region. AlibabaCloud::addHost('Ecs', 'ecs.cn-beijing.aliyuncs.com', 'cn-beijing'); AlibabaCloud::addHost('Ecs', 'ecs.cn-hangzhou.aliyuncs.com', 'cn-hangzhou'); // Specify a global endpoint for ECS. If no region ID is specified, the global endpoint is used. AlibabaCloud::addHost('Ecs', 'ecs.cn-hangzhou.aliyuncs.com'); $request = Ecs::v20140526()->describeRegions(); $result = $request ->debug(true) ->regionId('cn-hangzhou') // Specify the region ID. ->host("ecs.cn-hangzhou.aliyuncs.com") // Specify the endpoint with the highest priority. ->request(); print_r($result->toArray()); } catch (ClientException $exception) { // Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, the information is displayed for reference only. echo $exception->getMessage() . PHP_EOL; } catch (ServerException $exception) { // Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, the information is displayed for reference only. echo $exception->getMessage() . PHP_EOL; echo $exception->getErrorCode() . PHP_EOL; echo $exception->getRequestId() . PHP_EOL; echo $exception->getErrorMessage() . PHP_EOL; } -
Cloud service SDK-based addressing mode. The endpoint is obtained from the endpoint.json file of a cloud service, such as ECS.
-
Core library-based addressing mode. The endpoint is obtained from the Data.php data file in the core library.
-
Addressing by calling the location-based service API.
If none of the preceding modes return an endpoint, an exception occurs with the message "No host found for {product} in the {regionId}, you can specify host by host() method. Like $request->host('xxx.xxx.aliyuncs.com')".