如何使用file_get_contents在php中的远程Web服务器上获取gzip的页面?

Rob*_*Rob 1 php gzip file-get-contents

我试图通过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();
Run Code Online (Sandbox Code Playgroud)

这是有效的,但我希望有一种方法可以使用file_get_contents代替它.

谢谢.

Jua*_*ano 5

你试过这个吗?

$content = file_get_contents('http://example.com',false,$context);
Run Code Online (Sandbox Code Playgroud)