Skip to content

Instantly share code, notes, and snippets.

@sotoz
Created December 14, 2015 12:50
Show Gist options
  • Save sotoz/2bdda68983fa08b05ebd to your computer and use it in GitHub Desktop.
Save sotoz/2bdda68983fa08b05ebd to your computer and use it in GitHub Desktop.
Fetch image with php
private 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_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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment