dosc9472 2012-04-02 20:33
浏览 41
已采纳

找出url是否被压缩

I just have a question thats been pozzling my head all morning.

I want to get all http headers thru CURL ... and find out, if pinged url is compressed, or not. (either one of: gzip / x-gzip / deflate / compress)

But all I can get from CURL is this:

[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 like "Content-encoding" ... any chance to get that thru PHP?

  • 写回答

1条回答 默认 最新

  • dongyan6235 2012-04-02 21:22
    关注

    You have to get the headers from curl and parse them for the Content-Encoding header.

    Here is an example:

    <?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("
    ",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";
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏