Question

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

Was it helpful?

Solution 2

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.

OTHER TIPS

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

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.

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,'">';

Are you sure it's not working? Your code is working fine for me (after adding the missing semicolon after $image = ...).

The reason it might be giving you trouble is because it's not actually an image, it's an MJPEG. It uses an HTTP session that's kept open and with a multipart content (similar to what you see in MIME email), and the server pushes a new JPEG frame to replace the last one on an interval. CURL seems to be happy just giving you the first frame though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top