How do I use file_get_contents to get a gzip'e

2019-01-20 18:04发布

I'm trying to receive a gzip'ed version of a page through file_get_contents in php 5.2.9

I was able to do it using fopen with the following code:

    $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();

That works, but I was hoping there was a way I could do it using file_get_contents instead.

Thanks.

2条回答
女痞
2楼-- · 2019-01-20 18:29
We Are One
3楼-- · 2019-01-20 18:41

Have you tried this?

$content = file_get_contents('http://example.com',false,$context);
查看更多
登录 后发表回答