Le SDK for PHP V1.0 permet de configurer un proxy réseau au niveau de la requête, au niveau du client ou via des variables d'environnement.
Remarque
L'ordre de priorité décroissant pour la configuration du proxy est le suivant : paramètres au niveau de la requête, paramètres au niveau du client, puis variables d'environnement.
-
Configurez un proxy directement dans une requête.
<?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; } -
Définissez un proxy au niveau du 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 { // 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; } -
Utilisez des variables d'environnement pour appliquer la configuration du proxy.
HTTP_PROXY ou http_proxy
HTTPS_PROXY ou https_proxy
NO_PROXY ou no_proxy