1

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.

RotS
  • 2,142
  • 2
  • 24
  • 30
  • 1
    This particular server is possibly filtering scripting, using some kind of user agents white list. Try to pass in the user-agent of your browser, as first try. There is possibly other way of filtering server side. Does it work using `file_get_contents() ` ? Also allow url fopen must be enabled into the php ini – NVRM Dec 08 '21 at 14:16
  • 1
    file_get_contents() returns empty also. I've checked that allow url fopen is On with phpInfo(). I will try the user agent trick. – RotS Dec 08 '21 at 14:28
  • 1
    I still get the same error with the user agent trick `curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)');` – RotS Dec 08 '21 at 14:41

3 Answers3

0

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;
chaos505
  • 468
  • 3
  • 10
0

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);
NVRM
  • 11,480
  • 1
  • 88
  • 87
  • If this code is working on your side, then I must have some particular settings preventing me from retrieving a file from outside of my domain. – RotS Dec 08 '21 at 15:33
  • Yes, at least, I do confirm it works straight away as such. Maybe check your write permissions. Try as first https://stackoverflow.com/questions/2900690/how-do-i-give-php-write-access-to-a-directory Check your php logs – NVRM Dec 08 '21 at 15:38
0

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.

RotS
  • 2,142
  • 2
  • 24
  • 30