How to set CURLOPT_PROXY in Guzzle right way? How to set CURLOPT_PROXY in Guzzle right way? curl curl

How to set CURLOPT_PROXY in Guzzle right way?


I spent numerous hours finding out myself that you will also need to set the option CURLOPT_HTTPPROXYTUNNEL to 1. So something like:

<?php$res = $client->get('http://www.ipmango.com/api/myip', ['config' => [    'curl' => [        'CURLOPT_PROXY' => '194.135.220.18:8081',        'CURLOPT_HTTPPROXYTUNNEL' => 1,    ]   ]]);


GuzzleHttp Client automatically detects if environment variables HTTP_PROXY and HTTPS_PROXY are set.

(see lines 165-175 of \path\to\project\vendor\guzzlehttp\guzzle\src\Client.php)

So set both HTTP_PROXY=http://ip:port and HTTPS_PROXY=https://ip:port as system environment variables. Now restart your command line, and rerun php artisan serve

You may be able to configure authentication for your proxy in the environment variable as well, if it's required


Guzzle docs give info about setting proxy for a single request

$client->request('GET', '/', ['proxy' => 'tcp://localhost:8125']);

http://docs.guzzlephp.org/en/latest/request-options.html#proxy

But you can set it to all requests when initializing client

    $client = new Client([        'base_uri' => 'http://doma.in/',        'timeout' => 10.0,        'cookie' => true,        'proxy' => 'tcp://12.34.56.78:3128',    ]);