Search code examples
phpimagecurlconnection-refused

php curl_exec() Connection refused when retrieving a remote image


I want to retrieve a remote hosted image with php. The image exists, I can access to it with my browser. However the result of the curl_exec() is empty and the curl_error() says:

Failed to connect to img107.xooimage.com port 80: Connection refused

Here is my code:

                $ch = curl_init();
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_URL, 'http://img107.xooimage.com/files/5/0/b/ss-2014-06-15-at-12.58.47--46324f5.png');
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
                curl_setopt($ch, CURLOPT_TIMEOUT, 10);
                $image = curl_exec($ch);

                if( empty($image) ){
                    echo("Impossible to retrieve the file !<br>
                    error: ".curl_error($ch)."<br>");
                }

If I can open the image with my browser, then why the connection is refused when I use curl?

Remark: it works for images from my own domain, but not with external images like the one in the example.

EDIT: It actually seems not to be a php problem, since I couldn't even perform a curl or a ping from the host server of my website:

curl http://img107.xooimage.com/files/5/0/b/ss-2014-06-15-at-12.58.47--46324f5.png > image.png
Connection Refused

ping http://img107.xooimage.com
ping: unknown host http://img107.xooimage.com

ping http://www.google.com
ping: unknown host http://www.google.com

Perhaps my hosting provider applied some limitations/firewalls.


Solution

  • This is probably not a happy conclusion, at most it's a workaround, but here is what I finally did.

    I exported all the images URL from my website database. I created a bash script based on this command:

    curl http://img107.xooimage.com/files/5/0/b/ss-2014-06-15-at-12.58.47--46324f5.png > image.png
    

    To save an image from the internet by using a curl. I ran the script from my personal computer which didn't have the same limitation as my website host. I could retrieve all the pictures.

    If you're not managing the rights/limitations on your host, you probably can't do much more.