'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 1:[1]
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.
Solution 2:[2]
You need to close the curl before reading it
$ch = curl_init('http://img107.xooimage.com/files/5/0/b/ss-2014-06-15-at-12.58.47--46324f5.png');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
$image = curl_exec($ch);
if (curl_errno($ch)) {
print_r(curl_error($ch));
die;
}
curl_close($ch);
return $image;
Solution 3:[3]
After testing, the following does save the given image into the file image.png
along the script, and is readable. Is it what was missing?
<?php
$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);
var_dump($image); // Binary content
$save = fopen('image.png', 'w');
fwrite($save, $image);
fclose($save);
curl_close($ch);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | RotS |
Solution 2 | chaos505 |
Solution 3 | NVRM |