All Products
Search
Document Center

Alibaba Cloud SDK:Configure a proxy

Last Updated:Jul 25, 2024

This topic describes how to configure a proxy in an SDK V2.0 for PHP.

Configuration methods

Note

Proxy configurations are effective in the following descending order: the proxy configured by using the RuntimeOptions parameter and the proxy configured by using the Config object.

  • Configure a proxy by using the RuntimeOptions parameter. The setting takes effect only on the requests that use the RuntimeOptions instance.

    <?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);
    
            // Configure a proxy by using the RuntimeOptions parameter. The setting takes effect only on the requests that use the RuntimeOptions instance.
            $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();
  • Configure a proxy by using the Config object when you initialize the client. The setting takes effect on all requests.

    <?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>
                // Configure a proxy.
                "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();