Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set proxy in Guzzle

I have a problem with set proxy in guzzle that a blank page was shown while with curl everything works perfect. The code that I used in guzzle and curl came below. What is wrong with this code: Guzzle:

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;

require_once "vendor/autoload.php";

try {
  $client = new Client();
  $request = new \GuzzleHttp\Psr7\Request('GET', 'http://httpbin.org');
  $response = $client->send($request, [
      'timeout'  => 30,
      'curl'  => [
          'CURLOPT_PROXY' => '*.*.*.*',
          'CURLOPT_PROXYPORT' => *,
          'CURLOPT_PROXYUSERPWD' => '*:*',
      ],

  ]);
  echo '</pre>';
  echo($response->getBody());
  exit;
} catch (RequestException $e) {
  echo $e->getRequest();
  if ($e->hasResponse()) {
      echo $e->getResponse();
  }
}

And The code with CURL:

$url = 'http://httpbin.org';
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_setopt($ch, CURLOPT_PROXY, '*.*.*.*');
curl_setopt($ch, CURLOPT_PROXYPORT, *);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, '*:*');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$page = curl_exec($ch);
echo $page;

Thanks.

like image 409
M Sh Avatar asked Sep 01 '15 06:09

M Sh


People also ask

What is guzzle proxy?

Setting proxy in Guzzle allows hiding a real IP server, modifying request-response on the proxy side, and statistics gathering by the proxy.

How do you set headers in guzzle?

Each key is the name of a header, and each value is a string or array of strings representing the header field values. // Set various headers on a request $client->request('GET', '/get', [ 'headers' => [ 'User-Agent' => 'testing/1.0', 'Accept' => 'application/json', 'X-Foo' => ['Bar', 'Baz'] ] ]);

Does guzzle use cURL?

Guzzle has historically only utilized cURL to send HTTP requests. cURL is an amazing HTTP client (arguably the best), and Guzzle will continue to use it by default when it is available. It is rare, but some developers don't have cURL installed on their systems or run into version specific issues.

How do I debug guzzle request?

Debugging when using Guzzle, is quiet easy by providing the debug key in the payload: $client->request('GET', '/url, ['debug' => true]); This is quiet easy and not an issue if your are not passing any body content, using only query string to dump what's been request.


2 Answers

As for Guzzle 6.

Guzzle docs give info about setting proxy for a single request

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

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',
    ]);

UPD. I don't know why, but I face a strange behaviour. One server with guzzle version 6.2.2 works great with config as above, and the other one with the same version receives 400 Bad Request HTTP error from a proxy. It is solved with another config structure (found in docs for guzzle 3)

$client = new Client([
    'base_uri' => 'http://doma.in/',
    'timeout' => 10.0,
    'cookie' => true,
    'request.options' => [
        'proxy' => 'tcp://12.34.56.78:3128',
    ],
]);
like image 134
shukshin.ivan Avatar answered Sep 23 '22 17:09

shukshin.ivan


for a proxy, if you have the username and password, you can use:

$client = new GuzzleHttp\Client();

$res = $client->request("POST", "https://endpoint.com", [
    "proxy" => "http://username:[email protected]:10",
]);

this worked with guzzle in php.

like image 29
Diego Santa Cruz Mendezú Avatar answered Sep 21 '22 17:09

Diego Santa Cruz Mendezú