1
代碼很簡單:爲什麼有時無法使用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-內容]:未能打開流)
當然2中上面的URL可用。 我也在php.ini中設置了allow_url_fopen = On
。
你試圖使用捲曲功能的替代的fopen /的file_get_contents? – 2014-12-15 14:40:14