function makeGoogleApiRequest($params)
{
$apiUrl = "https://maps.googleapis.com/maps/api/geocode/json";
$context = stream_context_create(['http'=>['timeout'=>5]]);
// Make request
$response = json_decode(file_get_contents($apiUrl . "?" . http_build_query($params), false, $context));
// Check response
if (
json_last_error() ||
!isset($response->status) ||
$response->status !== "OK" ||
!isset($response->results[0]->place_id) ||
!isset($response->results[0]->formatted_address) ||
!isset($response->results[0]->address_components) ||
!isset($response->results[0]->geometry->location->lat) ||
!isset($response->results[0]->geometry->location->lng)
) {
throw new \UnexpectedValueException ("Google Api Request Failed");
}
return $response;
}
/* Пример использования */
$place = makeGoogleApiRequest([
'address' => "Москва, Лаврушинский пер, 10",
'language' => "ru"
]);