GeoNames Home | Postal Codes | Download / Webservice | About 

GeoNames Forum
  [Search] Search   [Recent Topics] Recent Topics   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
PHP Extract data from geonames  XML
Forum Index -> General
Author Message
ezycc



Joined: 13/04/2010 14:26:43
Messages: 1
Offline

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!


kamm



Joined: 15/05/2009 09:14:20
Messages: 14
Offline

Try setting charset to UTF8 by putting the following line before anything else...

Code:
header('Content-type: text/html; charset=utf-8');

Albox | Mojacar
[WWW]
 
Forum Index -> General
Go to:   
Powered by JForum 2.1.5 © JForum Team