[英]PHP cURL not returning Content-Encoding header
When I run curl -I http://api.stackoverflow.com/1.1/badges
fro my terminal, it shows me the following headers: 当我在终端上运行
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
Yet, when I run the same cURL request through PHP, I get this: 但是,当我通过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] =>
)
The major difference that matters to me is that when run through PHP, I do not get the Content-Encoding
header, without which I do not know if the content needs to be gzip inflated or not. 对我而言,主要的区别在于,通过PHP运行时,没有获取
Content-Encoding
标头,没有它,我不知道是否需要对内容进行gzip Content-Encoding
。
Is there a way to get the Content-Encoding
header, or to check for gzip compression some other way? 有没有一种方法来获取
Content-Encoding
标头,或者以其他方式检查gzip压缩?
There is no header_response
nor accept-encoding
in the returned getinfo array. 返回的getinfo数组中没有
header_response
或accept-encoding
。 I thought CURLINFO_HEADER_OUT
on getinfo would give response headers, but only request headers are given. 我以为getinfo上的
CURLINFO_HEADER_OUT
会给出响应头,但是只给出请求头。
But you can get raw headers using the CURLOPT_HEADER
option set to true. 但是您可以使用设置为true的
CURLOPT_HEADER
选项获取原始标头。 So I suggest you to do something less natural : 所以我建议你做一些不太自然的事情:
$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
If you set CURLOPT_HEADER
to true
, curl returns the header alongside the body. 如果将
CURLOPT_HEADER
为true
,则curl将在正文旁边返回标头。 If you're just interested in the header, you can set CURLOPT_NOBODY
to true
and the body is not returned (which emulates the -I
flag on the command line). 如果只对标题感兴趣,可以将
CURLOPT_NOBODY
为true
并且不返回正文(它在命令行上模拟-I
标志)。
This example sets just the CURLOPT_HEADER
, reads the Content-Encoding
header (if it is set) and uncompresses the body: 本示例仅
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;
Disclaimer: gzdecode
might not be available in your PHP-version. 免责声明:
gzdecode
在您的PHP版本中可能不可用。 I've tested it with PHP 5.4.4 and it worked. 我已经用PHP 5.4.4对其进行了测试,并且可以正常工作。
You could also install the HTTP_Request2
-PEAR package which does that for you (plus you get easy access to the headers without HTTP-header parsing): 您还可以安装
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.