OK, so I was wondering if anyone could help, basically I want to extract airport names using the geonames service from an ICAO code.
This is working fine, except on some airports such as Geneva (LSGG)
It doesnt seem to be encoding the airport name with UTF8 and is showing some strange characters.
Heres my code so far....
Code:
<?
$socket = fsockopen('ws.geonames.org', 80, $errno, $errstr);
if(!$socket)
{
echo "SOCKET ERROR";
}
$icao = $_GET['icao'];
$page_url = "/search?q=$icao&maxRows=1&style=full&fcode=AIRP&lang=en";
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);
// Extract the XML code from response
$ret = substr($ret, strpos($ret, '<?xml'));
// Parse XML Code
$xml = simplexml_load_string($ret);
if(intval($xml->totalResultsCount) >= 1)
{
$name = $xml->geoname[0]->name;
$lat = $xml->geoname[0]->lat;
$lng = $xml->geoname[0]->lng;
$iata = $xml->geoname[0]->alternateName;
echo "$name<BR>$lat, $lng<BR>$iata<BR><br>";
echo "$ret </br>";
echo "<a href='http://ws.geonames.org$page_url'>Raw</a>";
} else {
die("ICAO Not found!");
}
?>
This works fine for airports such as Manchester EGCC but Geneva (LSGG) and Berlin Schoenfeld (EDDB) is giving strange outputs!
Thanks in advance for the help!