file_get_content 获取错误的网页 [英] file_get_content get the wrong web
问题描述
我正在学习使用 PHP-file_get_contents
抓取网站内容,但是出了点问题.我想要的网络是http://www.jandan.net".
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".
但是使用file_get_content()
,我从"http://i.jandan 获取内容.net"(这是电话页面,它们是不同的页面).user_agent
也无法使用.
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);
?>
推荐答案
$content = file_get_contents($url,);
中的逗号导致了问题.
Your comma in $content = file_get_contents($url,);
is causing the problem.
-------------------------------------------------------------------------^
-------------------------------------------------------------------------^
保留逗号会产生以下错误信息:
Keeping the comma will produce the following error message:
解析错误:语法错误,意外的')' in.....(文件夹路径等)
Parse error: syntax error, unexpected ')' in.....(folder path etc.)
快速说明:使用 $url = 'http://i.jandan.net/';
也可以,显示内容.
Quick note: Using $url = 'http://i.jandan.net/';
worked also, got content displayed.
试试这个:
<?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;
?>
这篇关于file_get_content 获取错误的网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!