gpt4 book ai didi

php - 在 HTTPS 期间访问 Guzzle 代理 header

转载 作者:行者123 更新时间:2023-12-03 21:33:32 24 4
gpt4 key购买 nike

我正在编写 Guzzle 中间件(版本 6.2.1)以允许出站 HTTP 请求的代理粘性 session 。例如,第一次访问代理服务器池会返回一个代理节点 ID header 。在后续请求中使用此 header 值将确保继续使用相同的节点。

这是我的问题:使用 HTTPS 时,代理节点 ID header 包含在初始代理连接 header 中。无论我尝试什么,我似乎都无法在 Guzzle 中访问它。这是在基于 PHP 原生 libcurl 的 Curl 下运行的典型事务:

HTTP/1.0 200 Connection established
Proxy-Node-ID: 12345

HTTP/1.1 200 OK
Date: Mon, 03 Oct 2016 00:06:04 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
P3P: CP="This is not a P3P policy! See https://www.google.com/support/accounts/answer/151657?hl=en for more info."
Server: gws
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Set-Cookie: NID=87=VP_ptZU47mOXvC10uU2Ue6UVpLi8p9ngovbLfViChxGjEupGx3UKh4QXi2dyU0QqBSwfgkR9nxgJGLQdnAm2adKWsAGvdzZCCwgC1kqJpc0ZE9BNaqT_FapqULWGitx0ZNQJpJfJYZIasQ; expires=Tue, 04-Apr-2017 00:06:04 GMT; path=/; domain=.google.com; HttpOnly
Alt-Svc: quic=":443"; ma=2592000; v="36,35,34,33,32"
Accept-Ranges: none
Vary: Accept-Encoding
Transfer-Encoding: chunked

我需要的 header 在第一个代理“已建立连接”部分“代理节点 ID”中。我可以使用 PHP 的 native curl 实现非常轻松地访问所有这些 header 。但遗憾的是,PHP 的原生 Libcurl 支持的 curl 缺乏我真正需要的 Guzzle 强大的中间件功能。

这是一个最小的例子。为了保持代码简短,我省略了我编写的中间件。 (很抱歉,我无法提供有效的代理用户/通行证):
require_once __DIR__ . '/vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Handler\CurlHandler;

$handler = new CurlHandler();
$stack = HandlerStack::create($handler);
$url = 'https://www.google.com/';

// Select proxy:
$proxy = 'http://'.'someuser:somepass' .'@'.'someproxy.com:12345';

// Instantiate client with options:
$client = new Client(['handler' => $stack,
'proxy' => $proxy,]);

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

$responseHeaders = $response->getHeaders();

foreach ($responseHeaders as $key => $values) {
$responseHeaders[$key] = implode(', ', $values);
}
var_dump($responseHeaders);

来自上述代码的 header 响应:
array(13) {
["Date"]=>
string(29) "Mon, 03 Oct 2016 01:35:55 GMT"
["Expires"]=>
string(2) "-1"
["Cache-Control"]=>
string(18) "private, max-age=0"
["Content-Type"]=>
string(29) "text/html; charset=ISO-8859-1"
["P3P"]=>
string(109) "CP="This is not a P3P policy! See https://www.google.com/support/accounts/answer/151657?hl=en for more info.""
["Server"]=>
string(3) "gws"
["X-XSS-Protection"]=>
string(13) "1; mode=block"
["X-Frame-Options"]=>
string(10) "SAMEORIGIN"
["Set-Cookie"]=>
string(214) "NID=87=ATOuQjSf6g_95LdzKPjeec1NGABDjgysBZF35AEwNK1YSGWe5nbvFot1Ju_f_H1vL2FAPYK26HhWWqePDfL2XlBxBUpMd0yDbqEdH6nST1qqesGl3nV-Hp1CTcLg_YhB; expires=Tue, 04-Apr-2017 01:35:55 GMT; path=/; domain=.google.co.uk; HttpOnly"
["Alt-Svc"]=>
string(43) "quic=":443"; ma=2592000; v="36,35,34,33,32""
["Accept-Ranges"]=>
string(4) "none"
["Vary"]=>
string(15) "Accept-Encoding"
["Transfer-Encoding"]=>
string(7) "chunked"
}

正如您所看到的,最初的“200 连接已建立”消息和“代理节点 ID” header 完全丢失。我需要找到某种方法来访问此 header 。

最佳答案

迟到总比我想的好。
我花了几个小时才让它工作。

$url = 'https://www.google.com/';
// Select proxy:
$proxy = 'http://'.'someuser:somepass' .'@'.'someproxy.com:12345';$headers = [];
$guzzleClient = new Client();
$options = [
'proxy' => $proxy,
\GuzzleHttp\RequestOptions::ON_HEADERS => function (ResponseInterface $response) use (&$headers) {
$headers = array_merge($headers, $response->getHeaders());
}
];
$response = $guzzleClient->get($url, $options);

关于php - 在 HTTPS 期间访问 Guzzle 代理 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39823638/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com