How do I use file_get_contents to get a gzip'ed page on a remote web server in php?

廉价感情. 提交于 2019-11-27 08:27:21

问题


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.


回答1:


Have you tried this?

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



回答2:


Try using 'compress.zlib://http://example.com'

Answer taken from here: How can I read GZIP-ed response from Stackoverflow API in PHP?



来源:https://stackoverflow.com/questions/3800147/how-do-i-use-file-get-contents-to-get-a-gziped-page-on-a-remote-web-server-in-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!