我正在使用 Goutte(内部使用 Guzzle)进行网络抓取项目。我正在开发一个自定义速率限制器,因此我将所有 HTTP 操作存储到一个针对 IP 的数据库表中,以便我可以检查是否在最近的时间范围内对主机进行了调用。
目前我正在使用gethostbyname将已知主机名转换为 IP 地址,但 Guzzle 已经完成了查找,因此这可能是浪费。此外,主机名可能解析为多个 IP 地址(因此需要gethostbynamel),因此我自己派生的 IP 实际上可能不是 Guzzle 使用的 IP 地址(尽管猜测,可能在 PHP 级别有一些缓存这将使它可能gethostbyname返回正确的结果)。
我为 Guzzle 订阅了一个插件,它从 cURL 返回一些非常有趣的数据,以实现这一点。可悲的是,IP 地址不在其中。必须有一种方法来实现这一点 - 有什么想法吗?
class HttpLoggerPlugin implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return array(
'request.complete' => 'onRequestComplete',
);
}
/**
* Handles the request complete event (for both success/failed)
*
* @param \Guzzle\Common\Event $event
*/
public function onRequestComplete(Event $event)
{
$request = $event['request'];
$host = $request->getHost();
$ip = gethostbyname($host);
$response = $event['response'];
$responseCode = $response ? $response->getStatusCode() : null;
// Try to get cURL data here
echo $response ? print_r($response->getInfo(), true) : null;
}
}
这就是$response->getInfo()回报:
Array(
[url] => http://example.com/page.html
[content_type] => text/html
[http_code] => 200
[header_size] => 228
[request_size] => 149
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 1.209516
[namelookup_time] => 0.559758
[connect_time] => 0.954811
[pretransfer_time] => 0.954916
[size_upload] => 0
[size_download] => 22390
[speed_download] => 18511
[speed_upload] => 0
[download_content_length] => 22390
[upload_content_length] => 0
[starttransfer_time] => 1.056913
[redirect_time] => 0
[certinfo] => Array()
[redirect_url] =>
)