I wrote a php-script today:
Maybe this can help someone:
Get XML code from Geonames (change this line: postalCodeSearch?postalcode=6616&country=CH):
Code:
// Get geonames XML Info, calling this script:"postcode.php?plz=6616" give you the XML for Losone
$socket = fsockopen('ws.geonames.org', 80, $errno, $errstr);
if(!$socket)
{
echo "SOCKET ERROR";
}
$page_url = "/postalCodeSearch?postalcode=6616&country=CH";
fputs($socket, "GET ".$page_url." HTTP/1.0\r\n");
fputs($socket, "Host: ws.geonames.org\r\n");
fputs($socket, "Connection: Close\r\n\r\n");
$ret = '';
while (!feof($socket))
{
$ret .= fgets($socket, 4096);
}
fclose($socket);
Parse XML:
Code:
// Extract the XML code from response
$ret = substr($ret, strpos($ret, '<?xml'));
// Parse XML Code
$xml = simplexml_load_string($ret);
Get Info:
Code:
if(intval($xml->totalResultsCount) >= 1)
{
$plz = $xml->code[0]->postalcode;
$name = $xml->code[0]->name;
$lat2 = $xml->code[0]->lat;
$lng2 = $xml->code[0]->lng;
echo "$plz, $name<BR>$lat2, $lng2<BR><BR>";
}
Get distance:
Code:
//lat,lng from Losone:
$lat1= 46.13;
$lng1= 8.75;
$dist = 0;
$a1 = deg2rad($lat1);
$b1 = deg2rad($lng1);
$a2 = deg2rad($lat2);
$b2 = deg2rad($lng2);
$d = acos(cos($a1) * cos($b1) * cos($a2) * cos($b2) + cos($a1) * sin($b1) * cos($a2) * sin($b2) + sin($a1) * sin($a2));
$dist = $d * 6372.0;
echo "from Losone:(lat,lng) (46.13, 8.75) = $dist km";
You can see the XML document by writing Code:
.
Hope this can help you