0

I have a problem with PHP curl library.

At this URL: http://i.ebayimg.com/00/s/NTAwWDUwMA==/z/7KcAAOxyRhBS3lhE/$_1.JPG?set_id=8800005007 there's an image. For "curl", image does not exist.

I'm very confused. Following my method:

public static function downloadImage($url, $pathToSave = "")
{
    $imageUrl = (string)$url;
    $ch       = curl_init($imageUrl);

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);

    $raw = curl_exec($ch);
    curl_close($ch);

    if(file_exists($pathToSave))
    {
        unlink($pathToSave);
    }

    $fp = fopen($pathToSave, 'x');
    fwrite($fp, $raw);
    fclose($fp);

    return $pathToSave;
}
double-beep
  • 5,031
  • 17
  • 33
  • 41
Raffaele Capasso
  • 209
  • 1
  • 2
  • 16
  • possible duplicate of [Downloading a large file using curl](http://stackoverflow.com/questions/6409462/downloading-a-large-file-using-curl) – Liam Sorsby Jan 28 '15 at 09:05
  • If you attempt a basic curl in ssh with the path it will not download unless you specify the -o flag. I would recommend looking at the above options set and try some of them. – Liam Sorsby Jan 28 '15 at 09:06

1 Answers1

2

This works for me:

<?php
    $c = curl_init();
    curl_setopt($c,CURLOPT_URL,"http://i.ebayimg.com/00/s/NTAwWDUwMA==/z/7KcAAOxyRhBS3lhE/$_1.JPG?set_id=8800005007");
    curl_setopt($c,CURLOPT_RETURNTRANSFER,true);
    $img = curl_exec($c);
    file_put_contents('image.jpg',$img);
?>

It is possible, that your server is configured differently, and you need to set proper user-agent, like this:

curl_setopt($c,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.91 Safari/537.36 OPR/27.0.1689.54');

... as many sites deny to serve files requested by CURL itself.

Flash Thunder
  • 11,672
  • 8
  • 47
  • 91