All Products
Search
Document Center

Alibaba Cloud SDK:Configure a proxy

Last Updated:Sep 19, 2024

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

Note

The priority levels of methods for configuring a proxy are listed in the following descending order: initiate a request, use a client, and configure an environment variable.

  • Initiate a request to configure a proxy.

    <?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 {
        // Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured. 
        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") // If you set the proxy parameter to https, set the scheme parameter to https. If you set the proxy parameter to http or no, set the scheme to http. Otherwise, the proxy does not take effect.
            ->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) {
        // Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, the information is displayed for reference only. 
        echo $exception->getMessage() . PHP_EOL;
    } catch (ServerException $exception) {
        // Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, the information is displayed for reference only. 
        echo $exception->getMessage() . PHP_EOL;
        echo $exception->getErrorCode() . PHP_EOL;
        echo $exception->getRequestId() . PHP_EOL;
        echo $exception->getErrorMessage() . PHP_EOL;
    }
  • Use a client to configure a proxy.

    <?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 {
        // Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured. 
        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") // If you set the proxy parameter to https, set the scheme parameter to https. If you set the proxy parameter to http or no, set the scheme to http. Otherwise, the proxy does not take effect.
            ->request();
        print_r($result->toArray());
    } catch (ClientException $exception) {
        // Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, the information is displayed for reference only. 
        echo $exception->getMessage() . PHP_EOL;
    } catch (ServerException $exception) {
        // Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, the information is displayed for reference only. 
        echo $exception->getMessage() . PHP_EOL;
        echo $exception->getErrorCode() . PHP_EOL;
        echo $exception->getRequestId() . PHP_EOL;
        echo $exception->getErrorMessage() . PHP_EOL;
    }
  • Configure an environment variable to configure a proxy.

    • HTTP_PROXY or http_proxy

    • HTTPS_PROXY or https_proxy

    • NO_PROXY or no_proxy