Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use proxy authentication with Goutte?

Tags:

php

guzzle

goutte

I have the following code but it always returns a 407 HTTP status code.

$url = 'http://whatismyip.org';

$client = new Client();

$options = array(
    'proxy' => array(
        'http'  => 'tcp://@x.x.x.x:8010',
    ),
    'auth' => array('d80fe9ebasab73d21a4', '', 'basic')
);

$crawler = $client->request('GET', $url, $options);

$status = $client->getResponse()->getStatus();

echo $status; // 407

I am using Goutte with Guzzle 6. I started off trying to set the proxy with setDefaultOption but this method has been deprecated.

My username and blank password is definitely correct as it works with curl on the command line:

curl -U d80fe9ebasab73d21a4: -vx x.x.x.x:8010 http://whatismyip.org/

I have spent several hours on this and I would appreciate any help!

like image 241
Abs Avatar asked Oct 03 '15 17:10

Abs


2 Answers

Have you tried to instantiate your Client with the proxy options directly?

Like this:

$url = 'http://whatismyip.org';

$client = new Client($url, array(
    'version'        => 'v1.1',
    'request.options' => array(
        'auth'    => array('d80fe9ebasab73d21a4', '', 'Basic'),
        'proxy'   => 'tcp://@x.x.x.x:8010'
    )
));


$crawler = $client->request('GET', $url);

$status = $client->getResponse()->getStatus();

echo $status; // 407
like image 122
Dennis Stücken Avatar answered Oct 02 '22 15:10

Dennis Stücken


$config = [
    'proxy' => [
        'http' => 'xx.xx.xx.xx:8080'
    ]
];

$client = new Client($config);
$client->setAuth('username', 'password', 'basic');

$crawler = $client->request('GET', $url);
$status = $client->getResponse()->getStatus();

echo $status;

I suppose it's a client configuration, not a request parameter.

like image 40
Michele Carino Avatar answered Oct 02 '22 15:10

Michele Carino