value array
if( is_array( $params ) ) {
foreach( $params as $param => $value ) {
$param_string .= "$param=$value&";
}
$param_string = rtrim( $param_string, "&" );
} else {
// or just use their string if they give it to us pre-joined.
$param_string = $params;
}
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $url);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle,CURLOPT_POST,1);
curl_setopt($curl_handle,CURLOPT_POSTFIELDS, $param_string );
curl_setopt($curl_handle, CURLOPT_FAILONERROR, True ); // Enable fail on HTTP error codes >= 400
$retval = curl_exec( $curl_handle );
if( curl_errno( $curl_handle ) > 0 ) { // run the whole process
if($debug) {
echo( "Error accessing url: $url: " . curl_error( $curl_handle ) );
}
curl_close($curl_handle);
return false;
} else {
curl_close($curl_handle);
return $retval;
}
}
// Simple curl wrapper to return any GET data or false if any failure.
// This is a suitable replacement for systems where fopen() isn't allowed
// to open remote files. (Which is sensible for security reasons.)
function get_data( $url ) {
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $url);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_FAILONERROR, True ); // Enable failover on HTTP error codes >= 400
$retval = curl_exec( $curl_handle );
if( curl_errno( $curl_handle ) > 0 ) { // run the whole process
if($debug) {
echo( "Error accessing url: $url: " . curl_error( $curl_handle ) );
}
curl_close($curl_handle);
return false;
} else {
curl_close($curl_handle);
return $retval;
}
}
/* This method takes an address in long form (a single string) and returns you either:
* the latitude, longitude and accuracy (in an array form). This is a thin wrapper
* around Google's geo-coding API.
* See: http://www.google.com/apis/maps/documentation/services.html#Geocoding for the
* service information, and see:
* http://www.google.com/apis/maps/documentation/reference.html#GGeoAddressAccuracy
* for what the accuracy constants mean.
*
* Note that this method is primitive. it doesn't do much in the way of error checking or
* accuracy checking. That should be done in a wrapper method for business critical applications.
*/
function get_lat_long_from_addr($address) {
// make sure no wayward ampersands make it into the address field..
$address = preg_replace("/&/", "and", $address);
$url = sprintf("http://maps.google.com/maps/geo?q=%s&output=xml&key=%s", urlencode($address), $googlemap_api_key);
if($debug) {
echo("Accessing URL: $url
\n");
}
$data = get_data($url);
if(!$data) {
if($debug) {
echo("Failed to retrieve lat/lon from Google.\n");
}
return False;
} else {
// We should have a fat XML blob now. On to pillage.
$geocoded = array();
if($debug) {
echo("
Raw XML from google\n\n"); echo($data . "
\n"); } $kml = new SimpleXMLElement(utf8_encode($data)); // utf data is not well supported here.. foreach($kml->Response->Placemark as $row) { $address = $row->address; $accuracy = (string)$row->AddressDetails['Accuracy']; // Lol type cast // I'm not sure what the 3rd parameter in the coordinates element is, so I don't store it. :?: // it should be noted that the XML returns the coordinates in reverse order as the CSV. list($long,$lat) = explode(',', $row->Point->coordinates); $geocoded[] = array( 'accuracy' => $accuracy, 'latitude' => $lat, 'longitude' => $long, 'addy' => $address ); if($debug) { echo("\n");
//var_dump($row);
echo("Accuracy: $accuracy
\n");
echo("Address: $address
\n");
echo("Lat: $lat
\n");
echo("Long: $long
\n");
echo("