2010-11-04 96 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

相关问题