5

As mentioned above, the php file_get_contents() function or even the fopen()/fread() combination stucks and times out when trying to read this simple image url:

http://pics.redblue.de/artikelid/GR/1140436/fee_786_587_png

but the same image is easily loaded by browsers, whats the catch?

EDITED:

as requested in comments, I am showing the function I used to get the data:

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;
}

I also tried simply this:

echo file_get_contents('http://pics.redblue.de/artikelid/GR/1140436/fee_786_587_png');

Both give the same issue

EDITED:

As suggested in comment I used 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";

this is the result:

[DATA:]

[CODE:0]

[ERROR:]
Imran Ahmed
  • 790
  • 1
  • 9
  • 22
  • Is it fee_786_587_png or fee_786_587.png? – Ravi Hirani Feb 03 '16 at 11:32
  • Post the code you've tried. – Charlotte Dunois Feb 03 '16 at 11:32
  • it is _png, exactly as mentioned in the url – Imran Ahmed Feb 03 '16 at 11:33
  • Works perfectly with `file_get_contents` on my server. `$image = file_get_contents('http://pics.redblue.de/artikelid/GR/1140436/fee_786_587_png'); header('Content-Type: image/png'); die($image);` – Charlotte Dunois Feb 03 '16 at 11:36
  • @ImranAhmed: As you know every valid image has some extension such as .png, .jpg. gif .jpeg etc. – Ravi Hirani Feb 03 '16 at 11:36
  • oh it means it could be an issue with the calling server...??? – Imran Ahmed Feb 03 '16 at 11:36
  • Well, you do need to tell the browser you are printing a PNG.... add `header('Content-Type: image/png');` before your echo. – Charlotte Dunois Feb 03 '16 at 11:38
  • 1
    @CharlotteDunois you were right abt the server thing, I placed the same code on another of my server and it works there – Imran Ahmed Feb 03 '16 at 11:38
  • workt well on http://phpfiddle.org/ but as @CharlotteDunois said, don't just plainly echo the binary content of the image (����) – online Thomas Feb 03 '16 at 11:39
  • no need for header() as the first server does not even print garbage, its not that I want to print the image and I cannot, I even cannot get the image data on my first server, I mean the actual problem is that the 1st server is suffering a timeout – Imran Ahmed Feb 03 '16 at 11:39
  • Try getting the image with cURL and check with `curl_error` for errors. – Charlotte Dunois Feb 03 '16 at 11:40
  • nice suggestion @CharlotteDunois, let me try that – Imran Ahmed Feb 03 '16 at 11:41
  • Just change fee_786_587_png to fee_786_587_png.png. May be extension is removed by htaccess – Ravi Hirani Feb 03 '16 at 11:43
  • 1
    @RaviHirani That won't solve his issue, most likely the remote server won't even serve the image anymore. It's not an extension issue. Neither PHP nor the browser cares if the file has an extension or not, the browser only needs to know what that file is and everything works out. On PHP you just need to handle the data right and if you output it, send the correct header. – Charlotte Dunois Feb 03 '16 at 11:45
  • @CharlotteDunois updated the question with my latest curl attempt – Imran Ahmed Feb 03 '16 at 11:50
  • @CharlotteDunois: Your suggestion works perfect. – Ravi Hirani Feb 03 '16 at 11:53
  • @CharlotteDunois I think I owe you for pointing out that its the server that is the issue, although I am not able to find what is blocking it, but I atleast found out that the server is responsible for that. You can write an Answer and I will accept it! – Imran Ahmed Feb 03 '16 at 12:03

1 Answers1

1

If you don't get the remote data with file_get_contents, you can try it with cURL as it can provide error messages on curl_error. If you get nothing, even no error, then something on your server blocks outgoing connections. Maybe you even want to try curl over SSH. I'm not sure if that makes any difference but it's worth the try. If you don't get anything you may want to consider contacting the server admin (if you're not that) or the provider.

Charlotte Dunois
  • 4,638
  • 2
  • 20
  • 39