1

当我curl -I http://api.stackoverflow.com/1.1/badges从终端运行时,它会显示以下标题:

HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 42804
Content-Type: application/json; charset=utf-8
Content-Encoding: gzip
X-AspNetMvc-Version: 4.0
X-RateLimit-Max: 300
X-RateLimit-Current: 297
X-AspNet-Version: 4.0.30319
Set-Cookie: .ASPXBrowserOverride=; expires=Mon, 08-Oct-2012 04:29:28 GMT; path=/
Date: Tue, 09 Oct 2012 04:29:27 GMT

然而,当我通过 PHP 运行相同的 cURL 请求时,我得到了这个:

Array
(
    [url] => http://api.stackoverflow.com/1.1/badges?10102
    [content_type] => application/json; charset=utf-8
    [http_code] => 200
    [header_size] => 277
    [request_size] => 85
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.168343
    [namelookup_time] => 0.023417
    [connect_time] => 0.046293
    [pretransfer_time] => 0.046365
    [size_upload] => 0
    [size_download] => 42804
    [speed_download] => 254266
    [speed_upload] => 0
    [download_content_length] => 42804
    [upload_content_length] => 0
    [starttransfer_time] => 0.097563
    [redirect_time] => 0
    [certinfo] => Array
        (
        )

    [redirect_url] => 
)

对我来说重要的主要区别是,当通过 PHP 运行时,我没有得到Content-Encoding标题,没有它我不知道内容是否需要 gzip 膨胀。

有没有办法获取Content-Encoding标题,或者以其他方式检查 gzip 压缩?

4

2 回答 2

5

返回的getinfo数组中没有header_response也没有。我认为getinfo 会给出响应头,但只给出请求头。accept-encodingCURLINFO_HEADER_OUT

CURLOPT_HEADER但是您可以使用设置为 true的选项来获取原始标头。所以我建议你做一些不那么自然的事情:

$curl = curl_init();

$opts = array (
        CURLOPT_URL => 'http://api.stackoverflow.com/1.1/badges',
        CURLOPT_TIMEOUT => 120,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_ENCODING => 'gzip',
        CURLOPT_HEADER => true,
);
curl_setopt_array($curl, $opts);

$return = curl_exec($curl);

list($rawHeader, $response) = explode("\r\n\r\n", $return, 2);

$cutHeaders = explode("\r\n", $rawHeader);
$headers = array();
foreach ($cutHeaders as $row)
{
    $cutRow = explode(":", $row, 2);
    $headers[$cutRow[0]] = trim($cutRow[1]);
}

echo $headers['Content-Encoding']; // gzip
于 2012-10-09T05:56:06.520 回答
3

如果设置CURLOPT_HEADERtrue,curl 会在正文旁边返回标题。如果您只对标头感兴趣,您可以设置CURLOPT_NOBODYtrue并且不返回正文(它模拟-I命令行上的标志)。

此示例仅设置CURLOPT_HEADER,读取Content-Encoding标头(如果已设置)并解压缩正文:

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, "http://api.stackoverflow.com/1.1/badges");
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($curl);
curl_close($curl);

list($header, $body) = explode("\r\n\r\n", $response, 2);
if(preg_match('@Content-Encoding:\s+(\w+)@i', $header, $match)) {
    switch (strtolower($match[1])) {
        case 'gzip':
            $body = gzdecode($body);
        break;

        case 'compress':
            $body = gzuncompress($body);
        break;

        case 'deflate':
            $body = gzdeflate($body);
        break;
    }
}
echo $header;
echo $body;

免责声明:gzdecode可能在您的 PHP 版本中不可用。我已经使用 PHP 5.4.4 对其进行了测试,并且可以正常工作。

您还可以安装HTTP_Request2-PEAR 包,它会为您执行此操作(此外,您无需进行 HTTP 标头解析即可轻松访问标头):

include 'HTTP/Request2.php';
$request  = new HTTP_Request2('http://api.stackoverflow.com/1.1/badges',
    HTTP_Request2::METHOD_GET);

$response = $request->send();

echo $response->getBody();
于 2012-10-09T05:32:34.347 回答