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;
}
2
  • possible duplicate of Downloading a large file using curl Commented Jan 28, 2015 at 9: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. Commented Jan 28, 2015 at 9:06

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.