2010-11-04 100 views
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

+0

你試圖使用捲曲功能的替代的fopen /的file_get_contents? – 2014-12-15 14:40:14

回答

0

首先 - 你不需要urlencode完整的網址!只得到參數:

echo file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?'.http_build_query(array(
    'format' => 'text', 
    'ip'  => '119.97.23.59' 
))); 

你應該分享第二件事情是錯誤信息(如果有的話)

+0

附上我的錯誤消息,有幫助嗎? – DiveInto 2010-11-06 06:38:13

相關問題