全部产品
Search
文档中心

Alibaba Cloud SDK:Konfigurasikan periode timeout

更新时间:Jun 28, 2025

Anda dapat mengonfigurasi periode timeout yang sesuai untuk memastikan permintaan diproses dalam jangka waktu tertentu, mengurangi konsumsi sumber daya, serta meningkatkan ketahanan dan keandalan program. Topik ini menjelaskan cara mengonfigurasi periode timeout di Alibaba Cloud SDK untuk PHP V1.0.

Metode konfigurasi

Catatan

Tingkat prioritas metode untuk mengonfigurasi periode timeout adalah sebagai berikut (dari tertinggi ke terendah): memulai permintaan, menggunakan klien, dan menggunakan pengaturan default.

  • Gunakan pengaturan default. Periode timeout default untuk permintaan koneksi adalah 5 detik, sedangkan periode timeout default untuk permintaan baca adalah 10 detik.

  • Mulai permintaan untuk mengonfigurasi periode timeout.

    <?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 {
        // Pastikan variabel lingkungan ALIBABA_CLOUD_ACCESS_KEY_ID dan ALIBABA_CLOUD_ACCESS_KEY_SECRET telah dikonfigurasi.
        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) // Setel periode timeout untuk permintaan koneksi menjadi 5 detik.
            ->timeout(10) // Setel periode timeout untuk permintaan baca menjadi 10 detik.
            ->request();
        print_r($result->toArray());
    } catch (ClientException $exception) {
        // Tangani pengecualian dengan hati-hati dalam skenario bisnis aktual dan jangan pernah abaikan pengecualian dalam proyek Anda. Dalam contoh ini, informasi ditampilkan hanya untuk referensi.
        echo $exception->getMessage() . PHP_EOL;
    } catch (ServerException $exception) {
        // Tangani pengecualian dengan hati-hati dalam skenario bisnis aktual dan jangan pernah abaikan pengecualian dalam proyek Anda. Dalam contoh ini, informasi ditampilkan hanya untuk referensi.
        echo $exception->getMessage() . PHP_EOL;
        echo $exception->getErrorCode() . PHP_EOL;
        echo $exception->getRequestId() . PHP_EOL;
        echo $exception->getErrorMessage() . PHP_EOL;
    }
  • Gunakan klien untuk mengonfigurasi periode timeout.

    <?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 {
        // Pastikan variabel lingkungan ALIBABA_CLOUD_ACCESS_KEY_ID dan ALIBABA_CLOUD_ACCESS_KEY_SECRET telah dikonfigurasi.
        AlibabaCloud::accessKeyClient(getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'), getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET'))
            ->asDefaultClient()
            ->connectTimeout(5) // Setel periode timeout untuk permintaan koneksi menjadi 5 detik.
            ->timeout(10); // Setel periode timeout untuk permintaan baca menjadi 10 detik.
        $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) {
        // Tangani pengecualian dengan hati-hati dalam skenario bisnis aktual dan jangan pernah abaikan pengecualian dalam proyek Anda. Dalam contoh ini, informasi ditampilkan hanya untuk referensi.
        echo $exception->getMessage() . PHP_EOL;
    } catch (ServerException $exception) {
        // Tangani pengecualian dengan hati-hati dalam skenario bisnis aktual dan jangan pernah abaikan pengecualian dalam proyek Anda. Dalam contoh ini, informasi ditampilkan hanya untuk referensi.
        echo $exception->getMessage() . PHP_EOL;
        echo $exception->getErrorCode() . PHP_EOL;
        echo $exception->getRequestId() . PHP_EOL;
        echo $exception->getErrorMessage() . PHP_EOL;
    }