找出url是否被压缩

时间:2012-04-02 20:33:57

标签: php

我整个上午都有一个问题一直困扰着我。

我希望通过CURL获取所有http标头...并找出,如果pinged url是否已压缩。 (以下任一项:gzip / x-gzip / deflate / compress)

但我从CURL得到的只是:

[url] => http://www.url
[content_type] => text/html; charset=UTF-8
[http_code] => 302
[header_size] => 764
[request_size] => 54
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.042535
[namelookup_time] => 0.002779
[connect_time] => 0.014986
[pretransfer_time] => 0.015039
[size_upload] => 0
[size_download] => 0
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => 218
[upload_content_length] => 0
[starttransfer_time] => 0.042204
[redirect_time] => 0`

Nothnig喜欢"内容编码" ......有机会通过PHP获得这个吗?

1 个答案:

答案 0 :(得分:1)

您必须从curl获取标头并为Content-Encoding标头解析它们。

以下是一个例子:

<?php
$ch = curl_init("http://stackoverflow.com/");
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Accept-Encoding: gzip, deflate'));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$buffer = curl_exec($ch);
$curl_info = curl_getinfo($ch);
curl_close($ch);
$header_size = $curl_info["header_size"];
$headers = substr($buffer, 0, $header_size);
$body = substr($buffer, $header_size);


function getEncoding(&$headers){
    $arr=explode("\r\n",trim($headers));
    array_shift($arr);
    foreach($arr as $header){
        list($k,$v)=explode(':',$header);
        if ('content-encoding'==strtolower($k)){
            return trim($v);
        }
    }
    return false;
}   

$encoding=getEncoding($headers);

if ($encoding) {
    echo "Using: ".$encoding;
}else{
    echo "None";
}

?>