// bust the map element data into coords
$coords = FabrikString::mapStrToCoords($data);
// get the maps API key
$config = JComponentHelper::getParams('com_fabrik');
$apiKey = $config->get('google_api_key', '');
// build the reverse geocode api url
$url = 'https://maps.googleapis.com/maps/api/geocode/json?latlng=' . $coords->lat . ',' . $coords->long . '&sensor=false&key=' . $apiKey;
// make the HTTP request
$data = @file_get_contents($url);
// if you are doing this from a local host without certs, you may need to comment out the previous line and uncomment these lines
//$aContext = array(
// 'ssl' => array(
// 'verify_peer' => false,
// ),
//);
//$cxContext = stream_context_create($aContext);
//$data = @file_get_contents($url, false, $cxContext);
// parse the json response
$data = json_decode($data,true);
// loop around returned results, return false if we find a country code that isn't 'US'
foreach ($data['results'] as $result) {
foreach ($result['address_components'] as $component) {
foreach ($component['types'] as $type) {
if ($type === 'country') {
if ($component['short_name'] !== 'US') {
return false;
}
}
}
}
}
// we got this far, so ... woo hoo ...
return true;