×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: Сергей Шевченко
Added: Apr 21, 2017 10:09 AM
Modified: May 23, 2017 3:58 PM
Views: 22
Tags: api google json
  1.     private function makeGoogleApiRequest($place)
  2.     {
  3.         $apiUrl = 'https://maps.googleapis.com/maps/api/geocode/json';
  4.         $context = stream_context_create(['http' => ['timeout' => 5]]);
  5.  
  6.         // Make request
  7.         $response = json_decode(file_get_contents($apiUrl . '?' . http_build_query($place), false, $context));
  8.  
  9.         // Check response
  10.         if (json_last_error() || 'OK' !== $response->status) {
  11.             return null;
  12.         }
  13.  
  14.         $geoInfo = [
  15.             'lat' => $response->results[0]->geometry->location->lat,
  16.             'lng' => $response->results[0]->geometry->location->lng,
  17.         ];
  18.  
  19.         return $geoInfo;
  20.     }
  21.