开发者

Why sometimes can't get content using file_get_contents()?

开发者 https://www.devze.com 2023-01-23 17:01 出处:网络
The code is simple: <?php function getStringFromUrl($url){ $fResource = fopen($url, \'r\'); do { $data = fread($fResource, 8192);

The code is simple:

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

Sometimes I can get content, sometimes not. I can't figure out why.

(EDIT:the error msg is :[function.fopen]: failed to open stream and [function.file-get-contents]: failed to open stream)

Of course the 2 URL above are available. I have also 开发者_运维问答set the allow_url_fopen = On in php.ini.


First of all - you don't need to urlencode full url! Only GET parameters:

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

Second thing you should share with is error message (if any)

0

精彩评论

暂无评论...
验证码 换一张
取 消