本文為您介紹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();