moh*_*iee 9 php proxy curl guzzle
我在guzzle中设置代理有问题,显示空白页面,而卷曲一切都很完美.我在guzzle和curl中使用的代码如下.这段代码有什么问题: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();
}
}
Run Code Online (Sandbox Code Playgroud)
和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;
Run Code Online (Sandbox Code Playgroud)
谢谢.
shu*_*van 14
至于Guzzle 6.
Guzzle文档提供有关为单个请求设置代理的信息
$client->request('GET', '/', ['proxy' => 'tcp://localhost:8125']);
Run Code Online (Sandbox Code Playgroud)
但是,您可以在初始化客户端时将其设置为所有请求
$client = new Client([
'base_uri' => 'http://doma.in/',
'timeout' => 10.0,
'cookie' => true,
'proxy' => 'tcp://12.34.56.78:3128',
]);
Run Code Online (Sandbox Code Playgroud)
UPD.我不知道为什么,但我面临一种奇怪的行为.一个使用guzzle版本6.2.2的服务器与上面的配置相同,而另一个具有相同版本400 Bad Request的服务器从代理接收HTTP错误.它通过另一个配置结构解决(在guzzle 3的文档中找到)
$client = new Client([
'base_uri' => 'http://doma.in/',
'timeout' => 10.0,
'cookie' => true,
'request.options' => [
'proxy' => 'tcp://12.34.56.78:3128',
],
]);
Run Code Online (Sandbox Code Playgroud)
对于代理,如果您有用户名和密码,则可以使用:
$client = new GuzzleHttp\Client();
$res = $client->request("POST", "https://endpoint.com", [
"proxy" => "http://username:password@192.168.16.1:10",
]);
Run Code Online (Sandbox Code Playgroud)
这在 php 中与 guzzle 一起使用。
小智 5
对于Guzzle6,我认为最好的方法是实现一个设置代理的中间件。
来自 Guzzle6 文档:
我们可以设置代理如下:
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());
Run Code Online (Sandbox Code Playgroud)
小智 3
psr-7 的过程可能有所不同,但如果您使用标准方法来实例化客户端,
路径\to\project\vendor\guzzlehttp\guzzle\src\Client.php,第 164-170 行包含读取环境变量的代码,以查看当前计算机上是否设置了 HTTP_PROXY 和 HTTPS_PROXY,如果设置了,Guzzle 将使用这些变量价值观。
此外,我必须设置 HTTPS_PROXY = http://ip:port(不是 https),因为我们的工作场所代理似乎通过 http 协议处理 https 和 http 请求。
此配置的优点是您不必更改源代码中的代理设置。
| 归档时间: |
|
| 查看次数: |
18342 次 |
| 最近记录: |