【问题标题】:getting content of page with 'br' encoding and decoding it by php curl使用'br'编码获取页面内容并通过php curl对其进行解码
【发布时间】:2018-12-23 01:36:02
【问题描述】:

我想通过php curl获取this page的内容:

我的卷曲示例:

function curll($url,$headers=null){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);


    if ($headers){

        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    }

    curl_setopt($ch, CURLOPT_ENCODING, '');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0');
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);

    $response = curl_exec($ch);

    $res['headerout'] = curl_getinfo($ch,CURLINFO_HEADER_OUT);
    $res['rescode'] = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if ($response === false) {
        $res['content'] = $response;
        $res['error'] = array(curl_errno($ch),curl_error($ch));
        return $res;
    }

    $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    $res['headerin'] = substr($response, 0, $header_size);
    $res['content'] = substr($response, $header_size);

    return $res;

}

回复:

array (size=4)
  'headerout' => string 'GET /wallets HTTP/1.1
Host: www.cryptocompare.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: br
Accept-Language: en-US,en;q=0.5
Connection: keep-alive
Upgrade-Insecure-Requests: 1

' (length=327)
  'rescode' => string '200' (length=3)
  'content' => boolean false
  'error' => 
    array (size=2)
      0 => int 23
      1 => string 'Unrecognized content encoding type. libcurl understands deflate, gzip content encodings.' (length=88)

响应编码为br,响应内容为false

我知道使用gzipdeflate 作为编码会给我一个内容。但是,我想到的内容只有br编码显示。

我在this page 上读到Curl V7.57.0 支持Brotli Compression 功能。我目前安装了7.59.0 版本,但Curl 在接收br encoding 中的内容时遇到错误。

现在我想知道如何获取带有br 编码的页面内容并通过 php curl 解压缩?

【问题讨论】:

  • 如果响应为假,$res['error'] 会得到什么?
  • @NigelRen 抱歉,我更新了问题
  • 您提到的页面谈论的是 Curl 本身,而不是 PHP 中的 Curl 实现。此外,在您的代码中,您设置 curl_setopt($ch, CURLOPT_ENCODING, '');根据文档php.net/manual/en/function.curl-setopt.php 的意思是:“发送包含所有支持的编码类型的标头” - 它说的是:“身份”、“放气”和“gzip”。请注意,此处未提及“br”,因此不支持 Brotli 解压缩。但也许现在这已经过时了?找出方法是:检查您的 PHP 安装使用的 curl 版本:php.net/manual/en/function.curl-version.php
  • 有一个可用于 PHP 的 Brotli 扩展,来自:github.com/kjdev/php-ext-brotli - 也许这可以帮助您解决问题?
  • 我自己,通过将用户代理字符串设置为不支持 Brotli 的值,我能够让上游服务器向我发送没有 brotli 编码的内容。检查caniuse.com/#feat=brotli 找出哪些不是(提示:我将它设置为 Chrome 49 - 它的现有 UA 列在:myip.ms/view/comp_browsers/4301/Google_Chrome_49.html

标签: php curl compression brotli


【解决方案1】:

如果您使用 cloudflare,那么您可以尝试从 cloudflare 禁用 brotli 扩展。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-07
    • 2011-11-14
    • 2010-12-31
    • 1970-01-01
    • 2015-01-17
    • 2012-06-04
    • 2022-01-02
    • 1970-01-01
    相关资源
    最近更新 更多