Jump to content

Retrieving XML data from Dynamic XML file


klous-1

Recommended Posts

Hi everyone,

 

I'm simply trying to grab a simple element from an XML.

 

The XML has the following format:

 

 

 

<geonames>
<geoname>
<toponymName>Grand Tikal Futura Hotel</toponymName>
<name>Grand Tikal Futura Hotel</name>
<lat>14.618</lat>
<lng>-90.5249</lng>
<geonameId>6475052</geonameId>
<countryCode>GT</countryCode>
<countryName>Guatemala</countryName>
<fcl>S</fcl>
<fcode>HTL</fcode>
<distance>1.23124</distance>
</geoname>
</geonames>

 

My PHP is currently like this (note the link to the XML is calling two PHP variables as part of it's URL)

 

<?PHP
header('Content-Type: text/plain');

$userId = '-495015601289433027';

if ( $_GET['user'] ) {
if ( is_numeric( $_GET['user'] ) )
$userId = $_GET['user'];
else
exit('This isn\'t a valid user id.');
}

$url = 'http://www.google.com/latitude/apps/badge/api?user='.$userId.'&type=json';

// We get the content
$content = file_get_contents( $url );

// We convert the JSON to an object
$json = json_decode( $content );

$coord = $json->features[0]->geometry->coordinates;
$timeStamp = $json->features[0]->properties->timeStamp;

if ( ! $coord )
exit('This user doesn\'t exist.');

$date = date( 'Y-m-d H:i', $timeStamp );
$lat = $coord[1];
$lon = $coord[0];

$sXML = new SimpleXMLElement('http://api.geonames.org/findNearby?lat=$lat&lng=$lon&username=klous&style=short', NULL, TRUE);

   print_r ($sXML);



?>

But with this I get an error: 

 

[message] => error parsing parameter

                    [value] => 14

 

I've been trying and searching all morning, lots of different ideas, but with no luck.  At the moment, for simplicity, I'm simply trying to output the needed field "GeonameId".  I would really appreciate some help, and apologies.... I am, quite obviously, a novice!  Thanks!

So I got the script working.....

 

<?PHP
header('Content-Type: text/plain');

$userId = '-USERNOHERE######';

if ( $_GET['user'] ) {
if ( is_numeric( $_GET['user'] ) )
$userId = $_GET['user'];
else
exit('This isn\'t a valid user id.');
}

$url = 'http://www.google.com/latitude/apps/badge/api?user='.$userId.'&type=json';

// We get the content
$content = file_get_contents( $url );

// We convert the JSON to an object
$json = json_decode( $content );

$coord = $json->features[0]->geometry->coordinates;
$timeStamp = $json->features[0]->properties->timeStamp;

if ( ! $coord )
exit('This user doesn\'t exist.');

$date = date( 'Y-m-d H:i', $timeStamp );
$lat = $coord[1];
$lon = $coord[0];
echo '<param name="flashvars" value="location_id=gn:';
$sXML = new SimpleXMLElement('http://api.geonames.org/findNearby?lat='.$lat.'&lng='.$lon.'&username=klous&style=short', NULL, TRUE);


foreach($sXML as $geo) // loop through our records
{
        echo 
$geo->geonameId;
        }

echo'&landscape=airport&time_format=12&us=uk&background=#666666&mini_momentBar=false&copyright_bar=false"><a href="http://yowindow.com/weatherwidget.php" style="width: 180px; height: 140px; display: block; text-indent: -50000px; font-size: 0px; background: #DDF url("http://yowindow.com/img/logo") no-repeat scroll 50% 50%">';
echo $lat.$lon;

?>

 

Only to find that actually, the geonameId isn't available on the "Findnearbyweather" so essentially, this is useless. :'(  What I'd wanted to do, was using google Latitude to update my location, also update the location on my little weather widget....alas....

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.