duanpa1898
duanpa1898
2015-06-17 07:17

无法通过在php中使用file_get_content获取网站内容

已采纳

When i am trying to get the website content from the external url fanpop.com by using file_get_contents in php, i am getting empty data. I used the below code to get the contents

$add_url= "http://www.fanpop.com/";  
$add_domain = file_get_contents($add_url);  
echo $add_domain;  

but here i am getting empty result for $add_domain. But the same code is working for other urls and i tried to send the request from browser not from the script then also it is not working.

Below is the same request, but in CURL:

error_reporting(-1);
ini_set('display_errors','On');
$url="http://www.fanpop.com/";
$ch = curl_init();

$header=array('GET /1575051 HTTP/1.1',
'Host: adfoc.us',
'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language:en-US,en;q=0.8',
'Cache-Control:max-age=0',
'Connection:keep-alive',
'Host:adfoc.us',
'User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4)            AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36',
);

curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,0);
curl_setopt( $ch, CURLOPT_COOKIESESSION, true );

curl_setopt($ch,CURLOPT_COOKIEFILE,'cookies.txt');
curl_setopt($ch,CURLOPT_COOKIEJAR,'cookies.txt');
curl_setopt($ch,CURLOPT_HTTPHEADER,$header);

echo $result=curl_exec($ch);

curl_close($ch);

... but the above is also not working, can any one tell is there any any changes have to make in that?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • doukao8851 doukao8851 6年前

    The problem with this particular site is that it only serves compressed contents and throws a 404 error otherwise.

    Easy fix:

    $ch = curl_init('http://www.fanpop.com');
    curl_setopt($ch,CURLOPT_ENCODING , "");
    curl_exec($ch);
    

    You can also make this work for file_get_contents() but with a substantial amount of effort, as described in this article.

    点赞 评论 复制链接分享