i need to save the images returned from the google places API to my server.
I've tried :
$url= 'https://maps.googleapis.com/maps/api/place/photo?key=API_KEY_HERE&photo_reference=CoQBcwAAAAtXfnhO3WCagOlY4p4Px7N8Pcks_N-cLbMQzruT-AdWRZoyJgABVUUcuZ_4bbcUdReloBl2zGd80W4E4w-N_kyab4xz4S3ZZIRVECCJP1u7JsXOfxEsx4XQbL-HeMBRKzNul0XSdy-Dv4495i_8-SqYqTBZMaLvn1YLaVM3aAzOEhBgLV4lpKeM39L6gb9wBbU6GhSmCkYp8djpm9_iaqkS93z4ekLnNg&maxheight=200'
$target = md5($array['result']['photos'][0]['photo_reference']);
$newname = 'img/googlephotos/newimage'.$target;
file_put_contents($newname,file_get_contents($url));
But it's not working - when it's saving it saves an empty file and not the image - I think it is missing the file extension but I'm not sure how to get it. does anyone know of any ways to save google places photo?
I'm using PHP to write my code.
Any help would be great
You should be aware of the restrictions in Terms of Service. Particularly, paragraph 10.5 (d) says:
You will not pre-fetch, cache, index, or store any Content to be used outside the Service, except that you may store limited amounts of Content solely for the purpose of improving the performance of your Maps API Implementation due to network latency (and not for the purpose of preventing Google from accurately tracking usage), and only if such storage:
is temporary (and in no event more than 30 calendar days); is secure;
does not manipulate or aggregate any part of the Content or Service; and
does not modify attribution in any way.
https://developers.google.com/maps/terms#10-license-restrictions
At this point it looks like the ToS doesn't allow saving images on your server.
You would first need to use the Google Places Photo API to get the Image Resource:
https://maps.googleapis.com/maps/api/place/photo?maxwidth=[width]&photoreference=[photo_reference]&key=[apiKEY]
It should return on status:200 an actual image resource that you can save using:
file_put_contents({{path}}, {{image resource}});
Responding to your question I try to do something similar than you but with the redirect url (the secon one). As you know when you wants to get a photo url from Google Places you have to send this url:
$url = "https://maps.googleapis.com/maps/api/place/photo?maxwidth=$maxwidth&photoreference=$reference&key=$api_key";
But then it redirects to that:
https://lh6.googleusercontent.com/-n5VUsX_0WSA/VU3vvlOKX3I/AAAAAAAAEN8/yg9TAhSbvCwC8lA3bB56SjcUcAx3zXYtQCLIB/s1600-w800/
What I do is first get the second url and then try to get the file from cURL but the result its similar than you, an empty file (0bytes) and I tried with different formats (jpg, png and gif)
function get_photos_from_google_places($reference){
$api_key = "YOUR API KEY";
$maxwidth = 800;
$url = "https://maps.googleapis.com/maps/api/place/photo?maxwidth=$maxwidth&photoreference=$reference&key=$api_key";
$furl = false;
// First check response headers
$headers = get_headers($url);
// Test for 301 or 302
if(preg_match('/^HTTP\/\d\.\d\s+(301|302)/',$headers[0]))
{
foreach($headers as $value)
{
if(substr(strtolower($value), 0, 9) == "location:")
{
$furl = trim(substr($value, 9, strlen($value)));
}
}
}
// Set final URL
$furl = ($furl) ? $furl : $url;
//Save the photo file to directory
$ch = curl_init($furl);
$fp = fopen('./files/media/image.png', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
echo $furl;
echo "<br><hr>";
echo $fp; die();
}
Maybe this it can gives you some idea. If someone knows how to do it it will gratefull.
Thank you very much,
This worked for me.
$query = "https://maps.googleapis.com/maps/api/place/photo?maxwidth=800&photoreference=PHOTO_REF&key=API_KEY";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $query);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$a = curl_exec($ch);
$url = Null;
if(preg_match('#Location: (.*)#', $a, $r)) {
$url = trim($r[1]);
}