Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to cURL image. Not sure what to do

Tags:

php

curl

We've gotten permission to periodically copy a webcam image from another site. We use cURL functions elsewhere in our code, but when trying to access this image, we are unable to.

I'm not sure what is going on. The code we use for many other cURL functions is like so:

$image = 'http://island-alpaca.selfip.com:10202/SnapShotJPEG?Resolution=640x480&Quality=Standard'    

$options = array(
                    CURLOPT_URL => $image,
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_FOLLOWLOCATION => true,
                    CURLOPT_CONNECTTIMEOUT => 120,
                    CURLOPT_TIMEOUT => 120,
                    CURLOPT_MAXREDIRS => 10
                );

        $ch = curl_init();
        curl_setopt_array($ch, $options);
        $cURL_source = curl_exec($ch);
        curl_close($ch);

This code doesn't work for the following URL (webcam image), which is accessible in a browser from our location: http://island-alpaca.selfip.com:10202/SnapShotJPEG?Resolution=640x480&Quality=Standard

When I run a test cURL, it just seems to hang for the length of the timeout. $cURL_source never has any data.

I've tried some other cURL examples online, but to no avail. I'm assuming there's a way to build the cURL request to get this to work, but nothing I've tried seems to get me anywhere.

Any help would be greatly appreciated.

Thanks

like image 289
edeneye Avatar asked May 13 '14 15:05

edeneye


4 Answers

I don't see any problems with your code. You can get error sometimes because of different problems with network. You can try to wait for good response in loop to increase the chances of success.

Something like:

$image = 'http://island-alpaca.selfip.com:10202/SnapShotJPEG?Resolution=640x480&Quality=Standard';
$tries = 3; // max tries to get good response
$retry_after = 5; // seconds to wait before new try

while($tries > 0) {
    $options = array(
        CURLOPT_URL => $image,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_CONNECTTIMEOUT => 10,
        CURLOPT_TIMEOUT => 10,
        CURLOPT_MAXREDIRS => 10
    );

    $ch = curl_init();
    curl_setopt_array($ch, $options);
    $cURL_source = curl_exec($ch);
    curl_close($ch);

    if($cURL_source !== false) {
        break;
    }
    else {
        $tries--;
        sleep($retry_after);
    }
}
like image 181
Mikhail Gerasimov Avatar answered Oct 18 '22 14:10

Mikhail Gerasimov


Can you fetch the URL from the server where this code is running? Perhaps it has firewall rules in place? You are fetching from a non-standard port: 10202. It must be allowed by your firewall.

I, like the others, found it easy to fetch the image with curl/php.

like image 44
Julian Avatar answered Oct 18 '22 12:10

Julian


As it was said before, I can either see any problem with the code. However, maybe you should consider setting more timeout for the curl - to be sure that this slow loading picture finally gets loaded. So, as a possibility, try to increase CURLOPT_TIMEOUT to weird big number, as well as corresponding timeout for php script execution. It may help. Maybe, the best variant is to mix the previous author's variant and this one.

like image 1
Wladyslaw Brodsky Avatar answered Oct 18 '22 13:10

Wladyslaw Brodsky


I tried wget on the image URL and it downloads the image and then seems to hang - perhaps the server isn't correctly closing the connection.

However I got file_get_contents to work rather than curl, if that helps:

<?php
$image = 'http://island-alpaca.selfip.com:10202/SnapShotJPEG?Resolution=640x480&Quality=Standard';
$imageData = base64_encode(file_get_contents($image));
$src = 'data: '.mime_content_type($image).';base64,'.$imageData;
echo '<img src="',$src,'">';
like image 1
logic-unit Avatar answered Oct 18 '22 12:10

logic-unit