How do I download an external image off a url with cURL?
3
-
stackoverflow.com/questions/1898711/…DampeS8N– DampeS8N2010-11-29 16:06:25 +00:00Commented Nov 29, 2010 at 16:06
-
Any reason why would you choose cURL over file_get_contents() ?Michal M– Michal M2010-11-29 16:06:34 +00:00Commented Nov 29, 2010 at 16:06
-
I am not sure that I will have fopen enabled on my server, so I am avoiding using file_get_contents().moteutsch– moteutsch2010-11-29 17:53:25 +00:00Commented Nov 29, 2010 at 17:53
Add a comment
|
2 Answers
See here:
http://www.edmondscommerce.co.uk/php/php-save-images-using-curl/
function save_image($img,$fullpath){
$ch = curl_init ($img);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec($ch);
curl_close ($ch);
if(file_exists($fullpath)){
unlink($fullpath);
}
$fp = fopen($fullpath,'x');
fwrite($fp, $rawdata);
fclose($fp);
}
Other articles/sources:
http://forums.digitalpoint.com/showthread.php?t=371632
Comments
from php.net - also reads into a string.
<?php
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "example.com");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
?>