Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么有时可以';无法使用文件获取内容?获取内容()?_Php_Url_File Get Contents - Fatal编程技术网

Php 为什么有时可以';无法使用文件获取内容?获取内容()?

Php 为什么有时可以';无法使用文件获取内容?获取内容()?,php,url,file-get-contents,Php,Url,File Get Contents,代码很简单: <?php function getStringFromUrl($url){ $fResource = fopen($url, 'r'); do { $data = fread($fResource, 8192); if (strlen($data) == 0) { break; } $contents .= $data; } while

代码很简单:

<?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');

首先-您不需要对完整url进行url编码!仅获取参数:

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

第二件您应该分享的是错误消息(如果有)

您是否尝试过使用curl函数而不是fopen/file\u get\u内容?