php file_get_contents()在加载图片时遇到困难

Imr*_*med 5 php fopen file-get-contents fread

如上所述,当尝试读取这个简单的图像url时,php file_get_contents()函数甚至fopen()/ fread()组合都会卡住并超时:

http://pics.redblue.de/artikelid/GR/1140436/fee_786_587_png
Run Code Online (Sandbox Code Playgroud)

但是同样的图像很容易被浏览器加载,最重要的是什么?

编辑:

根据评论中的要求,我显示了用于获取数据的函数:

function customRead($url)
{
    $contents = '';

    $handle = fopen($url, "rb");

    $dex = 0;

    while ( !feof($handle) )
    {
        if ( $dex++ > 100 )
            break;

        $contents .= fread($handle, 2048);
    }

    fclose($handle);

    echo "\nbreaking due to too many calls...\n";

    return $contents;
}
Run Code Online (Sandbox Code Playgroud)

我也试过这个:

echo file_get_contents('http://pics.redblue.de/artikelid/GR/1140436/fee_786_587_png');
Run Code Online (Sandbox Code Playgroud)

两者都给出了同样的问题

编辑:

正如评论中所建议我使用curl:

$ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.1 Safari/537.11');
    $res = curl_exec($ch);
    $rescode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
    curl_close($ch) ;
    echo "\n\n\n[DATA:";
    echo $res;
    echo "]\n\n\n[CODE:";
    print_r($rescode);
    echo "]\n\n\n[ERROR:";
    echo curl_error($ch);
    echo "]\n\n\n";
Run Code Online (Sandbox Code Playgroud)

这是结果:

[DATA:]

[CODE:0]

[ERROR:]
Run Code Online (Sandbox Code Playgroud)

Cha*_*ois 1

如果您无法使用 获取远程数据file_get_contents,您可以尝试使用 cURL,因为它可以在 上提供错误消息curl_error。如果你什么也没得到,甚至没有错误,那么你的服务器上的某些东西就会阻止传出连接。也许您甚至想尝试curl通过 SSH。我不确定这是否有什么不同,但值得一试。如果您没有得到任何信息,您可能需要考虑联系服务器管理员(如果您不是管理员)或提供商。