I'm trying to fetch this image using cURL: http://images.egypt.souq.com/media/item/2013/02/27/49/97/32/8/item_L_4997328_1650476.jpg
Using this code:
public static function fetchUrl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch); //get curl response
curl_close($ch);
return $data;
}
this code is working with any image (that I tried at least) except for the above image, it returns false after consuming all the timeout's 50 seconds,
did anybody know why?
Specifying the userAgent for cURL solved my problem:
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
here I'm getting the userAgent from the request,
or I can even specify it manually like:
curl_setopt($ch, CURLOPT_USERAGENT, "spider");
This answer helped me: https://stackoverflow.com/a/6595108/905801
How about something like this. phpFiddle.
<?php
function data_uri($file, $mime)
{
$contents = file_get_contents($file);
$base64 = base64_encode($contents);
return ('data:' . $mime . ';base64,' . $base64);
}
?>
<img src="<?php echo data_uri('http://images.egypt.souq.com/media/item/2013/02/27/49/97/32/8/item_L_4997328_1650476.jpg','image/jpg'); ?>" />
Information from here.