用file_get_contents()遇到的问题

在采集网页的时候遇到了这个错误:
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节点的支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值