Configure a Proxy in Yii2’s CurlTransport
Select CurlTransport before passing CURLOPT_PROXY and proxy authentication options to a Yii2 HTTP client request.
Select CurlTransport before passing numeric CURLOPT options. Yii2's HTTP client defaults to StreamTransport, which does not become cURL because those constants are present:
$client = new \yii\httpclient\Client([
'transport' => \yii\httpclient\CurlTransport::class,
]);
$response = $client->createRequest()
->setMethod('GET')
->setUrl('https://api.example/items')
->setOptions([
CURLOPT_PROXY => 'http://proxy.example:8080',
CURLOPT_PROXYAUTH => CURLAUTH_BASIC,
CURLOPT_PROXYUSERNAME => $proxyUser,
CURLOPT_PROXYPASSWORD => $proxyPassword,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => 15,
])
->send();
Use a PHP runtime with its cURL extension. This example expects an HTTP proxy accepting Basic; match the proxy and transport's supported method.
CURLOPT_PROXYAUTH chooses intermediary authentication. CURLOPT_HTTPAUTH and API Authorization serve the destination instead.
Yii2's shared request options, including case-sensitive proxy and timeout, are another interface. They do not make arbitrary cURL constants portable across backends.
If Client.requestConfig provides defaults that must survive, use addOptions for per-request additions. The shown setOptions replaces that collection and can remove inherited choices.