【问题标题】:How do I use file_get_contents to get a gzip'ed page on a remote web server in php?如何使用 file_get_contents 在 php 中的远程 Web 服务器上获取 gzip 页面?
【发布时间】:2011-04-17 12:59:18
【问题描述】:

我正在尝试通过 php 5.2.9 中的 file_get_contents 接收 gzip 版本的页面

我可以使用 fopen 和以下代码来做到这一点:

    $opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n" .
              "Accept-Encoding: gzip\r\n"
  )
);

$context = stream_context_create($opts);
ob_start();
$fp = fopen('http://example.com', 'r', false, $context);
fpassthru($fp);
fclose($fp);
$content = ob_get_contents();
ob_end_clean();

这行得通,但我希望有一种方法可以使用 file_get_contents 来代替。

谢谢。

【问题讨论】:

    标签: php gzip file-get-contents


    【解决方案1】:

    你试过了吗?

    $content = file_get_contents('http://example.com',false,$context);
    

    【讨论】:

    • 谢谢,不知道我是怎么错过的 :)
    【解决方案2】:

    尝试使用 'compress.zlib://http://example.com'

    从这里得到的答案: How can I read GZIP-ed response from Stackoverflow API in PHP?

    【讨论】:

      猜你喜欢
      • 2012-05-06
      • 2011-06-07
      • 2016-08-28
      • 2014-05-06
      • 2013-04-05
      • 2014-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多