TYPO3 Backend Hook (i.e. geocoding)

EXT.local_conf.php

$GLOBALS ['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'][] = 'EXT:storefinder/Classes/BackendHooks/GeocodeAddress.php:GeocodeAddress';

EXT:Classes/BackendHooks/

class GeocodeAddress 
{
	protected $conf  = array();
	
	function processDatamap_afterDatabaseOperations ($status, $table, $id, $fieldArray, &$pObj) 
	{
		if($table == 'tx_storefinder_domain_model_storefinder') 
		{
			$id = (substr($id, 0, 3) == "NEW") ? $pObj->substNEWwithIDs[$id] : $id;
			$row = \TYPO3\CMS\Backend\Utility\BackendUtility::getRecord ($table, $id);
			$address = urlencode($row['sf_street']." ".$row['sf_housenumber'].",".$row['sf_zip']." ".$row['sf_city'].",".$row['sf_country']);
			$url = "http://maps.googleapis.com/maps/api/geocode/json?address=".$address."&sensor=false&language=de";	
			
			// $dataRaw = @file_get_contents($url);
			$curl = curl_init();
			curl_setopt($curl, CURLOPT_HEADER, 0);
			curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($curl, CURLOPT_URL, $url);
			$dataRaw = curl_exec($curl);
			curl_close($curl);

			$data = json_decode($dataRaw, true);

			if($data['status'] == 'OVER_QUERY_LIMIT') 
			{
				die($data['error_message']);
			}

			$fieldArray['sf_latitude'] = $data['results'][0]['geometry']['location']['lat'];
			$fieldArray['sf_longitude'] = $data['results'][0]['geometry']['location']['lng'];
			$update = $GLOBALS['TYPO3_DB']->exec_UPDATEquery('tx_storefinder_domain_model_storefinder','uid='.$id , $fieldArray); 
		}
	}
}