GeoNames Home | Postal Codes | Download / Webservice | About 

GeoNames Forum
  [Search] Search   [Recent Topics] Recent Topics   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
Parsing Result XML with PHP  XML
Forum Index -> General
Author Message
nkinc



Joined: 28/04/2007 07:30:41
Messages: 1
Offline

Hi all,

Geonames seems like a great service, and I am trying to do something that I think should be simple, but with no luck. I want to take the lat and lng values that are returned from a query like this: http://ws.geonames.org/search?name=KTEB&fcode=AIRP&.xml

...and save them into PHP variables like $latitude and $longitude. This is the code I have been trying to use ($airportcode is defined earlier in my code):

[code]$url = "http://ws.geonames.org/search?name=$airportcode&fcode=AIRP";
$pageair = file_get_contents($url);
$xmlair = new SimpleXMLElement($url2, NULL, TRUE);
$longitude = ($xmlair->geonames->geoname->lng);
$latitude = ($xmlair->geonames->geoname->lat);[/code]

I have also tried substituting the SimpleXMLElement line with

[code]$xmlair = @simplexml_load_file($url);[/code]
and
[code]$xmlair = @simplexml_load_string($pageair);[/code]

Neither work either. Is the XML file not "well formed" and therefore not being saved as a string to $pageair?

In a different part of my code I successfully use "new SimpleXMLElement" to call to the Google GeoCoding service, so it must be something isolated to geonames, I would think.

Any assistance is greatly appreciated.

Nick
marc



Joined: 08/12/2005 07:39:47
Messages: 4499
Offline

Hi Nick

Isn't the $xmlair already the root element of the document? Did you try without the 'geonames' part and directly reference the 'geoname' element?

like this :$xmlair->geoname->lng
(instead of $xmlair->geonames->geoname->lng)

Regards

Marc

[WWW]
adrianotiger



Joined: 23/05/2007 00:05:53
Messages: 2
Offline

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:
    echo $ret;
.

Hope this can help you
 
Forum Index -> General
Go to:   
Powered by JForum 2.1.5 © JForum Team