Forum Moderators: coopster

Message Too Old, No Replies

Fatal error: Cannot instantiate non-existent class: simplexmlelement

please help with small snippet of code

         

marcus76

11:30 pm on Apr 18, 2007 (gmt 0)

10+ Year Member



hi,

I get the error: Fatal error: Cannot instantiate non-existent class: simplexmlelement

with the following snippet of code:

// Desired address
$url = "http://maps.google.com/maps/geo?q=$mapaddress&output=xml&key=$key";
// Retrieve the URL contents
$page = file_get_contents($url);
// Parse the returned XML file
$xml = new SimpleXMLElement($page);
// Parse the coordinate string
list($longitude, $latitude, $altitude) = explode(",", $xml->Response->Placemark->Point->coordinates);
// Output the coordinates
echo "latitude: $latitude, longitude: $longitude <br />";

Later i find SimpleXMLElement is not supported under PHP 4.x so i find this code snippet.

$getpage = file_get_contents($url);
$getpage = utf8_encode($getpage);
$responsedoc = domxml_open_mem($getpage);
$response = $responsedoc->get_elements_by_tagname("Response");

if ($response!=null) {
$placemark = $response[0]->get_elements_by_tagname("Placemark");
if ($placemark!=null) {
$point = $placemark[0]->get_elements_by_tagname("Point");
if ($point!=null) {
$cds = $point[0]->get_content();
$c = explode(",", $cds);
$_coords['lon'] = $c[0];
$_coords['lat'] = $c[1];

return $_coords;

echo "$_coords";
}
}
}
return null;

I was wondering if someone can advise where this should fit in / replace what existing code i have and to echo out the lat / long co-ordinates as above.

Many thanks

Marcus

eelixduppy

12:58 am on Apr 19, 2007 (gmt 0)



My first question: do you have PHP 5? If not, you aren't going to be able to use simpleXML.

marcus76

6:14 am on Apr 19, 2007 (gmt 0)

10+ Year Member



nope, don't ahve PHP 5.x

"Later i find SimpleXMLElement is not supported under PHP 4.x so i find this code snippet. "

eelixduppy

10:48 am on Apr 19, 2007 (gmt 0)



Sorry, didn't see that before. If you want to echo out the values, it looks like you are trying to echo an array incorrectly. Try to change the following:

echo "$_coords";

To something like this:


echo 'lon: '.$_coords['lon'].'<br/>lat: '.$_coords['lat'];

Also, why are you echoing the data after you are returning it? I'd return it and then echo the returned result accordingly from where the function is called.

marcus76

9:55 am on Apr 20, 2007 (gmt 0)

10+ Year Member



tried it lastnight, it didn't echo anythin out?

any ideas...

eelixduppy

10:27 pm on Apr 20, 2007 (gmt 0)



Where did you put the code? It must be in the correct place (vars must be defined, etc...)