在采集网页的时候遇到了这个错误:
Warning: file_get_contents(http://localhost/caijiDemo/html/news/baoxian_2.html)
[function.file-get-contents]: failed to open stream:
由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。
in D:\AppServ\www\caijiDemo\list.php on line 23
这是因为系统的原因,系统不认识localhost是什么?它解析不了localhost,
也就是说系统不知道localhost就是本机的ip,所以file_get_contents()读取不了文件
举例:
$url="http://localhost/caijiDemo/html/news/baoxian_".$_GET["id"].".html";
$content=file_get_contents($url);
如何解决:打开c盘下的,windows->System32->drivers->etc里的hosts。
将里面的# 127.0.0.1 localhost 的#去掉。
或者将localhost换成127.0.0.1
注意:
1:读取整个文件还可以用file_get_contents()函数
2:如果读取的是一个外部文件需要php.ini中allow_url_fopen节点的支持。