function getPostcode($lat, $lng) {
$returnValue = NULL;
$ch = curl_init();
$url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${lng}&sensor=false";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
$json = json_decode($result, TRUE);
if (isset($json['results'])) {
foreach ($json['results'] as $result) {
foreach ($result['address_components'] as $address_component) {
$types = $address_component['types'];
if (in_array('postal_code', $types) && sizeof($types) == 1) {
$returnValue = $address_component['short_name'];
}
}
}
}
return $returnValue;
}
echo getPostcode(51.4691, -0.2963);