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 w
For Guzzle6, I think the best way is to implement a middleware for setting proxy.
From Guzzle6 docs:
We can set proxy as below:
use Psr\Http\Message\RequestInterface;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\Client;
use GuzzleHttp\Middleware;
use Util\Api;
function add_proxy_callback($proxy_callback) {
return function (callable $handler) use ($proxy_callback) {
return function (RequestInterface $request,$options) use ($handler,$proxy_callback) {
$ip = $proxy_callback();
$options['proxy'] = $ip;
return $handler($request,$options);
};
};
}
$stack = new HandlerStack();
$stack->setHandler(new CurlHandler());
$stack->push(add_proxy_callback(function() {
return Api::getIp(); //function return a ip
}));
$client = new Client(['handler'=>$stack]);
$response = $client->request('GET','http://httpbin.org/ip');
var_dump((string)$response->getBody());
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',
],
]);
Had the same problem right now , and all i needed to do was use curl array keys as constants instead of strings ..
$response = $client->send($request, [
'timeout' => 30,
'curl' => [
CURLOPT_PROXY => '*.*.*.*',
CURLOPT_PROXYPORT => *,
CURLOPT_PROXYUSERPWD => '*:*',
],
]);
See CURL options Keys , they are not strings anymore.
The procedure for psr-7 may be different, but if you're using the standard way to instantiate a client,
path\to\project\vendor\guzzlehttp\guzzle\src\Client.php, lines 164-170 includes code to read the environment variables to see if HTTP_PROXY and HTTPS_PROXY are set on the current machine, and if yes, Guzzle will use those values.
Additionally, I had to set my HTTPS_PROXY = http://ip:port (not https), because our workplace proxy seems to handle both https and http requests via the http protocol.
The advantage of this configuration is that you don't have to chnge proxy settings in your source code.
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:password@192.168.16.1:10",
]);
this worked with guzzle in php.
$response = \Drupal::httpClient()->post($settings['base_url'] . 'api/search/', [
'verify' => true,
'body' => $post_data,
'headers' => [
'Content-type' => 'application/json',
],
'curl' => [
CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1_2,
CURLOPT_PROXY => 'proxyip:58080'],
]
)->getBody()->getContents();
Set proxy/https in Guzzle and SSL its work perfect.