Goutte icon indicating copy to clipboard operation
Goutte copied to clipboard

Setting proxy

Open lincolnaleixo opened this issue 9 years ago • 10 comments

How can I set proxy in a easy way with that request?

$client = new Client(); $crawler = $client->request('GET', "http://www.google.com");

Thanks

lincolnaleixo avatar Jul 02 '15 05:07 lincolnaleixo

Bump, I'd like to know this too. I've tried following Guzzle docs but it's not working that way.

harisb avatar Mar 04 '16 21:03 harisb

@harisb @linkinkin

$config = [
    'proxy' => [
        'http' => 'xx.xx.xx.xx:8080'
        ]
    ];
$client = new \Goutte\Client;
$client->setClient(new \GuzzleHttp\Client($config));

daids avatar Mar 12 '16 03:03 daids

just what i needed, worked perfectly. Thanks alot.

alimakarfi avatar Aug 24 '16 07:08 alimakarfi

Thank you @daids . Also worked a treat for me!

Luke-West avatar Sep 07 '16 11:09 Luke-West

I was scared there for a second. I tried a lot of very close variations to this for the past 3-4 hours, about to give up and use raw curl, then found this. Thank You! Very elegant, simple solution.

JonahKlimack avatar Jan 24 '17 14:01 JonahKlimack

I think goutte should be updated to work more like guzzle in the sense of setting the proxy. For example, guzzle allows the following....

$guzzle = new GuzzleClient();
$response = $guzzle->request('GET', $url, ['proxy' => 'socks5://user:[email protected]:1080']);
echo $response->getBody();

and it works as expected.

But goutte doesn't pass along the proxy information to guzzle in the request method. Which is a shame when you want to initialize a single goutte instance and make multiple http requests while rotating through a list of proxies for each request.

So consider the following...

$goutte = new GoutteClient();
$goutte->setClient(new GuzzleClient(['proxy' => $proxy]));
echo $goutte->request('GET', 'https://ifconfig.co/')->html();

and run this with php script.php | grep -e '<code class="ip">.*</code>'

and you will get the ip address for the proxy server.

Now if you use the following script...

$goutte = new GoutteClient();
echo $goutte->request('GET', 'https://ifconfig.co/', ['proxy' => $proxy])->html();

Run that with the same command and you will get your public ip address instead of the proxy ip.

So goutte's request() method doesn't work the same as guzzle's request() method and if I want to change proxies between requests, I have to create a new guzzle instance each time and pass it to goutte.

Hopefully there is a plan to fix this in the future. Maybe someone can make this a feature request or just patch it and submit a pull request.

rspenc29 avatar Apr 10 '17 22:04 rspenc29

@rspenc29 The request implementation comes from symfony/browser-kit so if you would like this to be considered it is probably best to report it there...

That said - browser kit is intended to simulate a web browser without the HTTP layer. Goutte brings the HTTP layer which just happens to be Guzzle, and I can't really imagine symfony wanting to make this kind of change just to accommodate a single HTTP implementation.

As for changing proxies between requests - check out the middleware implementation offered by guzzle. This should allow you to hook in and make modifications on each request without having to create a new guzzle instance.

ssnepenthe avatar Oct 23 '17 22:10 ssnepenthe

Now It's working

        $url = 'https://api.myip.com';
        $client = new \Goutte\Client;
        $client->setClient(new \GuzzleHttp\Client(['proxy' => 'http://xx.xx.xx.xx:8080']));
        $get_html = $client->request('GET', $url)->html();
        var_dump($get_html);

Mashpy avatar Dec 23 '18 09:12 Mashpy

For recent versions use: Goutte Client instance (which extends Symfony\Component\BrowserKit\HttpBrowser)

use Symfony\Component\HttpClient\HttpClient;
use Goutte\Client;

$client = new Client(HttpClient::create(['proxy' => 'http://xx.xx.xx.xx:80']));
...

acollazo25 avatar Mar 06 '20 00:03 acollazo25

For recent versions use: Goutte Client instance (which extends Symfony\Component\BrowserKit\HttpBrowser)

use Symfony\Component\HttpClient\HttpClient;
use Goutte\Client;

$client = new Client(HttpClient::create(['proxy' => 'http://xx.xx.xx.xx:80']));
...

I tried your code, but I get this error :((

image image

namlephuong95 avatar Nov 25 '20 10:11 namlephuong95