代码很简单:
<?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
。
答案 0 :(得分:0)
首先 - 你不需要urlencode完整网址!只有GET参数:
echo file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?'.http_build_query(array(
'format' => 'text',
'ip' => '119.97.23.59'
)));
你应该分享的第二件事是错误信息(如果有的话)