user3755793
user3755793

Reputation: 13

Unable save image locally with PHP Curl

I am trying to use PHP CURL to save this image to my computer, but isnt working, any help would be appreciated. Image to Save: https://s.yimg.com/aw/api/res/1.2/MRhsVBj10l4TrtNg5j7eBA--/YXBwaWQ9eXR3YXVjdGlvbnNlcnZpY2U7aD0xNTA7cT04NTtzcj0xLjI7c3M9MS4yO3c9MjAw/http://nevec-img.zenfs.com/prod/tw_ec05-7/258e866a-b39a-471d-8e6c-62801cbccd9d.jpg

My Curl function:

function grab_image($url,$saveto){
        $ch = curl_init ($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
        $rawdata=curl_exec($ch);
        curl_close ($ch);
        if(file_exists($saveto)){
                ($saveto);
        }
        $fp = fopen($saveto,'x');
        fwrite($fp, $rawdata);
        fclose($fp);

}

I am pretty sure the permission for the place to save to is 777 because I can save image from other website without any problem.

Thanks again

Upvotes: 1

Views: 89

Answers (1)

Jens A. Koch
Jens A. Koch

Reputation: 41756

It's a HTTPS request, please add curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

If you want to know what's going on, add $errmsg = curl_error($ch); before curl_close, and then echo $errmsg;.

Upvotes: 1

Related Questions