file_get_content get the wrong web

你。 提交于 2019-12-11 10:29:24

问题


I am learning to spider website contents with PHP-file_get_contents,but something is wrong.The web I want is "http://www.jandan.net".

But use file_get_content(),I get the contents from "http://i.jandan.net" (it's phone page, they are different pages). user_agent is also unusable.

<?php
ini_set("user_agent","Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2) Gecko/20100301 Ubuntu/9.10 (karmic) Firefox/3.6");
$url = 'http://www.jandan.net/';
/*
$opt = array( 'http'=>array(
'method'=>"GET",
'header'=>"User-Agent: Mozilla/5.0\n"
)
);
$context = stream_context_create($opt);
*/
$content = file_get_contents($url);
echo var_dump($content);
?>

回答1:


Your comma in $content = file_get_contents($url,); is causing the problem.

-------------------------------------------------------------------------^

From original posted code ---^

Keeping the comma will produce the following error message:

Parse error: syntax error, unexpected ')' in.....(folder path etc.)

Quick note: Using $url = 'http://i.jandan.net/'; worked also, got content displayed.

Try this:

<?php
ini_set("user_agent","Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2) Gecko/20100301 Ubuntu/9.10 (karmic) Firefox/3.6");
$url = 'http://www.jandan.net/';

/*
$opt = array( 'http'=>array(
'method'=>"GET",
'header'=>"User-Agent: Mozilla/5.0\n"
)
);
$context = stream_context_create($opt);
*/
$content = file_get_contents($url);
echo var_dump($content);
// echo $content;
?>


来源:https://stackoverflow.com/questions/18362048/file-get-content-get-the-wrong-web

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