全部产品
Search
文档中心

阿里云SDK:代理配置

更新时间:Jun 27, 2024

本文为您介绍V2.0 PHP SDK代理的配置方式。

代理配置方式

说明

代理配置优先级:RuntimeOption配置 > Config配置,优先级依次降低。

  • 通过运行时参数(RuntimeOptions)配置代理,仅对使用了该运行时参数的请求有效。

    <?php
    
    require_once 'vendor/autoload.php';
    use AlibabaCloud\SDK\Ecs\V20140526\Ecs;
    use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
    use Darabonba\OpenApi\Models\Config;
    use AlibabaCloud\SDK\Ecs\V20140526\Models\DescribeInstancesRequest;
    
    class ProxyDemo
    {
    
        public static function main()
        {
            $config = new Config([
                "accessKeyId" => getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
                "accessKeySecret" => getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
                "endpoint" => "ecs.cn-beijing.aliyuncs.com", // <endpoint>
            ]);
            $client = new Ecs($config);
    
            // 运行时参数设置,仅对使用了该运行时参数的请求有效
            $runtime = new RuntimeOptions([
                "httpProxy" => "http://127.0.0.1:8080",
                // "httpsProxy" => "https://username:password@proxyServer:port",
                // "noProxy" => "127.0.0.1,localhost"
            ]);
            $describeInstancesRequest = new DescribeInstancesRequest([
                "regionId" => "cn-beijing"
            ]);
            $resp = $client->describeInstancesWithOptions($describeInstancesRequest, $runtime);
            var_dump($resp);
        }
    }
    ProxyDemo::main();
  • 在Client初始化时,通过Config配置代理,对所有请求都生效。

    <?php
    
    require_once 'vendor/autoload.php';
    use AlibabaCloud\SDK\Ecs\V20140526\Ecs;
    use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
    use Darabonba\OpenApi\Models\Config;
    use AlibabaCloud\SDK\Ecs\V20140526\Models\DescribeInstancesRequest;
    
    class ProxyDemo
    {
    
        public static function main()
        {
            $config = new Config([
                "accessKeyId" => getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
                "accessKeySecret" => getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
                "endpoint" => "ecs.cn-beijing.aliyuncs.com", // <endpoint>
                // 代理设置
                "httpProxy" => "http://127.0.0.1:8080",
                // "httpsProxy" => "https://username:password@proxyServer:port",
                // "noProxy" => "127.0.0.1,localhost"
            ]);
            $client = new Ecs($config);
    
            $runtime = new RuntimeOptions([]);
            $describeInstancesRequest = new DescribeInstancesRequest([
                "regionId" => "cn-beijing"
            ]);
            $resp = $client->describeInstancesWithOptions($describeInstancesRequest, $runtime);
            var_dump($resp);
        }
    }
    ProxyDemo::main();