简体   繁体   中英

Google Places APi photo

I am trying to render an image using php for my website that will dynamically display images straight from the Google Places API (Reference https://developers.google.com/places/web-service/photos?hl=en ) The image source looks like the following example:

https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference=CnRtAAAATLZNl354RwP_9UKbQ_5Psy40texXePv4oAlgP4qNEkdIrkyse7rPXYGd9D_Uj1rVsQdWT4oRz4QrYAJNpFX7rzqqMlZw2h2E2y5IKMUZ7ouD_SlcHxYq1yL4KbKUv3qtWgTK0A6QbGh87GB3sscrHRIQiG2RrmU_jF4tENr9wGS_YxoUSSDrYjWmrNfeEHSGSc3FyhNLlBU&key=API_KEY

If you visit this URL via your browser it will render an image and this is no longer the URL you see.

My issue is that when viewing the page source, this is exactly the image source URL you see which is bad because my key is then publicized. What is the proper way to render images while keeping my key private? I have searched the API and the web for this but to no avail. I did see some tricks using file_get_contents and file_put_contents but my web host does not allow that. Finally, saving the images is against the api rules so that is not an option.

Any tips would be appreciated. Thanks in advance.

You may send a HEAD-request to the particular URL and extract the content of the Location -header:

<?php

  $context  = stream_context_create(array('http' =>array('method'=>'HEAD')));
  $fp = fopen('desiredPlacesApiImgUrl', 'rb', false, $context);
  $meta = stream_get_meta_data($fp);

  if(isset($meta['wrapper_data'])){
    $location=preg_grep('@^\s*Location:@',$meta['wrapper_data']);

    if(count($location)){
       $imgUrl=trim(preg_replace('@^Location:@i','',reset($location)));
      die($imgUrl);//the desired img-url
    }

  }
  fclose($fp);

?>

But when file_get_contents isn't allowed on your server I'm afraid fopen also isn't allowed for external URLs.

Another option: use the Maps-Javascript-API, request the place and the response should contain the desired URL (without using any key).


Demo:

 function loadPlacePhotos() { var photos = document.querySelectorAll('img[data-place]'), service = new google.maps.places .PlacesService(document.createElement('div')); for (var i = 0; i < photos.length; ++i) { (function(photo) { service.getDetails({ placeId: photo.getAttribute('data-place') }, function(r) { if (r.photos.length) { google.maps.event.addDomListener(photo, 'click', function() { photo.setAttribute('src', r.photos[0].getUrl({ maxHeight: 100 })); photo.style.visibility = 'visible'; if (r.photos.length > 1) { r.photos.push(r.photos.shift()); photo.setAttribute('title', 'click to see next photo'); photo.style.cursor = 'pointer'; } else { google.maps.event.clearListeners(photo, 'click'); } }); google.maps.event.trigger(photo, 'click'); } }); }(photos[i])); } } 
 body::before { content: url(https://maps.gstatic.com/mapfiles/api-3/images/powered-by-google-on-white2.png); } img[data-place] { visibility: hidden; display: block; } 
 <ul> <li> Google: Mountain View <img data-place="ChIJj61dQgK6j4AR4GeTYWZsKWw" /> </li> <li> Google: Sydney <img data-place="ChIJN1t_tDeuEmsRUsoyG83frY4" /> </li> <li> Google: Berlin <img data-place="ChIJReW1rcRRqEcRaY3CuKBdqZE" /> </li> </ul> <script defer src="https://maps.googleapis.com/maps/api/js?v=3&libraries=places&callback=loadPlacePhotos"></script> 

The demo uses a custom attribute for images data-place which will be assigned to a placeId of a particular place. It parses these images, requests the photos and displays them(when there ar more than 1 photo the user can switch between the photos by clicking on the image )


However, it must not be a problem when your key is visible, set the allowed referers for your (browser)-key and you can use the key without a risk on your own domain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM