Reputation: 14142
I am trying to download a remote image using Curl, it downloads a file however when I attempt to open the image it on my mac - I get a 'could not be opened' message.
I can see the filename & extension are intact however somehow it hasn't saved as properly as the filesize is 177 bytes, yet i'm expecting the filesize to be around 3kb.
Can anyone suggest why this is? Is the remote site preventing me somehow from downloading the file? I've tried this same code with some other images on other sites and it works fine??
$url = 'http://www.fifaindex.com/static/FIFA16/images/crest/256/light/21.png';
$saveto = '21.png';
$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_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
$raw = curl_exec($ch);
curl_close ($ch);
if(file_exists($saveto)){
unlink($saveto);
}
$fp = fopen($saveto,'x');
fwrite($fp, $raw);
fclose($fp);
Upvotes: 2
Views: 1944
Reputation: 745
The website you try to get the image from, probably added some restriction so that if the image is called from outside the domain will not be served.
To get around that you can specify the referrer in your CURL options, setting it with the url of the site you want to get the image from.
In your case
curl_setopt($ch, CURLOPT_REFERER, "http://www.fifaindex.com");
I tried it myself on my local server and it worked.
Upvotes: 1