代理配置

更新时间:

本文为您详细介绍V1.0 SDK如何进行网络代理配置。

说明

代理配置方式优先级:Request设置 > Client设置 > 环境变量,优先级依次降低。

  • 通过Request设置代理。

    <?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 {
        // 请确保已设置环境变量ALIBABA_CLOUD_ACCESS_KEY_ID和ALIBABA_CLOUD_ACCESS_KEY_SECRET。
        AlibabaCloud::accessKeyClient(getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'), getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET'));
        $request = Ecs::v20140526()->describeRegions();
        $result = $request
            ->version('2014-05-26')
            ->product('Ecs')
            ->action('DescribeRegions')
            ->regionId('cn-hangzhou')
            ->host("ecs.cn-hangzhou.aliyuncs.com")
            ->scheme("http") // 当proxy设置为https时,请设置scheme为https;当proxy设置为http和no时,请设置scheme为http。否则代理不生效
            ->options([
                'proxy' => [
                    'http'  => 'http://localhost:8080', // Use this proxy with "http"
    //                'https' => 'http://localhost:9124', // Use this proxy with "https",
    //                'no' => ['example.com']    // Don't use a proxy with these
                ]
            ])
            ->request();
        print_r($result->toArray());
    } catch (ClientException $exception) {
        // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
        echo $exception->getMessage() . PHP_EOL;
    } catch (ServerException $exception) {
        // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
        echo $exception->getMessage() . PHP_EOL;
        echo $exception->getErrorCode() . PHP_EOL;
        echo $exception->getRequestId() . PHP_EOL;
        echo $exception->getErrorMessage() . PHP_EOL;
    }
  • 通过Client设置代理。

    <?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 {
        // 请确保已设置环境变量ALIBABA_CLOUD_ACCESS_KEY_ID和ALIBABA_CLOUD_ACCESS_KEY_SECRET。
        AlibabaCloud::accessKeyClient(getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'), getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET'))
            ->asDefaultClient()
            ->options([
                'proxy' => [
                    'http'  => 'http://localhost:8080', // Use this proxy with "http"
    //                'https' => 'http://localhost:9124', // Use this proxy with "https",
    //                'no' => ['localhost']    // Don't use a proxy with these
                ]
            ]);
        $request = Ecs::v20140526()->describeRegions();
        $result = $request
            ->version('2014-05-26')
            ->product('Ecs')
            ->action('DescribeRegions')
            ->regionId('cn-hangzhou')
            ->host("ecs.cn-hangzhou.aliyuncs.com")
            ->scheme("http") // 当proxy设置为https时,请设置scheme为https;当proxy设置为http和no时,请设置scheme为http。否则代理不生效
            ->request();
        print_r($result->toArray());
    } catch (ClientException $exception) {
        // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
        echo $exception->getMessage() . PHP_EOL;
    } catch (ServerException $exception) {
        // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
        echo $exception->getMessage() . PHP_EOL;
        echo $exception->getErrorCode() . PHP_EOL;
        echo $exception->getRequestId() . PHP_EOL;
        echo $exception->getErrorMessage() . PHP_EOL;
    }
  • 通过环境变量设置代理。

    • HTTP_PROXY或者http_proxy

    • HTTPS_PROXY或者https_proxy

    • NO_PROXY或者no_proxy