为什么有时无法使用 file_get_contents() 获取内容?
代码很简单:
<?php
function getStringFromUrl($url){
$fResource = fopen($url, 'r');
do {
$data = fread($fResource, 8192);
if (strlen($data) == 0) {
break;
}
$contents .= $data;
} while(true);
fclose ($fResource);
$contents = mb_convert_encoding($contents,'utf-8','gbk');
return $contents;
}
echo getStringFromUrl(urlencode('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=text&ip=119.97.23.59'));
echo file_get_contents('http://blog.sina.com.cn/rss/1400122351.xml');
有时可以获取内容,有时不能。我不明白为什么。
(编辑:错误消息是:[function.fopen]:无法打开流和[function.file-get-contents]:无法打开流)
当然上面 2 个 URL 可用。 我还在 php.ini 中设置了 allow_url_fopen = On
。
The code is simple:
<?php
function getStringFromUrl($url){
$fResource = fopen($url, 'r');
do {
$data = fread($fResource, 8192);
if (strlen($data) == 0) {
break;
}
$contents .= $data;
} while(true);
fclose ($fResource);
$contents = mb_convert_encoding($contents,'utf-8','gbk');
return $contents;
}
echo getStringFromUrl(urlencode('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=text&ip=119.97.23.59'));
echo file_get_contents('http://blog.sina.com.cn/rss/1400122351.xml');
Sometimes I can get content, sometimes not. I can't figure out why.
(EDIT:the error msg is :[function.fopen]: failed to open stream and [function.file-get-contents]: failed to open stream)
Of course the 2 URL above are available.
I have also set the allow_url_fopen = On
in php.ini.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先 - 您不需要对完整网址进行 urlencode!仅获取参数:
您应该分享的第二件事是错误消息(如果有)
First of all - you don't need to urlencode full url! Only GET parameters:
Second thing you should share with is error message (if any)