在HTTPS期间访问Guzzle代理标头

时间:2016-10-03 02:00:38

标签: php https proxy header guzzle

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

这是我的问题:使用HTTPS时,代理节点ID标头包含在初始代理连接标头中。无论我尝试什么,我似乎无法在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

我需要的标头是第一个代理“Connection established”部分,“Proxy-Node-ID”。我可以使用PHP的原生curl实现轻松访问所有这些头文件。但令人遗憾的是,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);

以上代码的标题回复:

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 Estab Estab Established”消息和“Proxy-Node-ID”标头完全丢失。我需要找到一些方法来访问这个标题。

1 个答案:

答案 0 :(得分:0)

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

$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);