You can configure an appropriate timeout period to ensure requests are processed within a period, reduce the resources that are consumed, and improve the robustness and reliability of programs. This topic describes how to configure a timeout period in Alibaba Cloud SDK for PHP V1.0.
Configuration methods
Note
The priority levels of methods for configuring a timeout period are listed in the following descending order: initiate a request, use a client, and use the default settings.
Use the default settings. The default timeout period for connection requests is 5 seconds and the default timeout period for read requests is 10 seconds.
Initiate a request to configure a timeout period.
<?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')) ->asDefaultClient(); $request = Ecs::v20140526()->describeRegions(); $result = $request ->scheme('https') ->version('2014-05-26') ->product('Ecs') ->action('DescribeRegions') ->regionId('cn-hangzhou') ->host("ecs.cn-hangzhou.aliyuncs.com") ->connectTimeout(5) // Set the timeout period for connection requests to 5 seconds. ->timeout(10) // Set the timeout period for read requests to 10 seconds. ->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; }Use a client to configure a timeout period.
<?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')) ->asDefaultClient() ->connectTimeout(5) // Set the timeout period for connection requests to 5 seconds. ->timeout(10); // Set the timeout period for read requests to 10 seconds. $request = Ecs::v20140526()->describeRegions(); $result = $request ->scheme('https') ->version('2014-05-26') ->product('Ecs') ->action('DescribeRegions') ->regionId('cn-hangzhou') ->host("ecs.cn-hangzhou.aliyuncs.com") ->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; }