问题
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