 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
11-18-2007, 09:53 PM
|
#1 (permalink)
|
|
The Visitor
Join Date: Nov 2007
Posts: 3
Thanks: 0
|
alternative to simplexml command in php4, HELP!!!
just found out my server doesn't have php5 and doesn't plan to for months.
am trying to make a geocoding parsing script (i am very newbie to all this)
there is a great php geocde script here - http://www.developer.com/db/article.php/3621981
but uses simplexml command (line 19) that is not used by php4
found this as a possible alternative for simplexml command-
http://www.shop24-7.info/32-0-simple...tive-php4.html
but being a newbie am clueless how to do it, but i tried to no avail....
PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> </head> <?php
class sxml { var $parser; var $error_code; var $error_string; var $current_line; var $current_column; var $data = array(); var $datas = array(); function parse($data) { $this->parser = xml_parser_create('UTF-8'); xml_set_object($this->parser, $this); xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1); xml_set_element_handler($this->parser, 'tag_open', 'tag_close'); xml_set_character_data_handler($this->parser, 'cdata'); if (!xml_parse($this->parser, $data)) { $this->data = array(); $this->error_code = xml_get_error_code($this->parser); $this->error_string = xml_error_string($this->error_code); $this->current_line = xml_get_current_line_number($this->parser); $this->current_column = xml_get_current_column_number($this->parser); } else { $this->data = $this->data['child']; } xml_parser_free($this->parser); }
function tag_open($parser, $tag, $attribs) { $this->datas[] =& $this->data; $this->data =& $this->data['child'][$tag][]; }
function cdata($parser, $cdata) { @$this->data['data'] .= $cdata; }
function tag_close($parser, $tag) { $this->data =& $this->datas[count($this->datas)-1]; array_pop($this->datas); } }
// Your Google Maps API key $key = "***have removed my google api key but it was in here!!!***";
// Desired address $address = "http://maps.google.com/maps/geo?q=411+Woody+Hayes+Drive,+Columbus,+OH&output=xml&key=$key";
//// Retrieve the URL contents // $page = file_get_contents($address); // // // Parse the returned XML file // $xml = new SimpleXMLElement($page); //Demo: Einsatz von sxml
$url=$address;
$xml_parser = new sxml; $src=implode ('', file ($url)); $xml_parser->parse($src); $xml=$xml_parser->data;
// print_r($xml); /// Parse the coordinate string list($longitude, $latitude, $altitude) = explode(",", $xml->Response->Placemark->Point->coordinates);
// Output the coordinates echo "Longitude: $longitude, Latitude: $latitude";
?>
<body> </body> </html>
Anyone able to spot the mistake???? just want it to echo the results, will then be using the extended script on page 2 of the first link to mysql it....
Please help.
thanks in advance
|
|
|
|
11-18-2007, 10:23 PM
|
#2 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
Can you attach the XML document to your post so that we can test it on our own servers, please. As sensibly you have removed your Google API key.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
06-03-2009, 05:52 PM
|
#3 (permalink)
|
|
The Visitor
Join Date: Jun 2009
Posts: 1
Thanks: 0
|
I ran into the same problem, also couldn't do file_get_contents(), so I'm doing page-scraping of the XML. Very dirty but quick and works until Google changes the XML response for the geocoding request. But by then I hope to have PHP 5 in place.
You will need to adjust this to work with your stuff, but this is more or less how I utilize it.
PHP Code:
//Geocoding start
$id = $row_Recordset1['id'];
$street = $row_Recordset1['strasse_nr'];
$city = $row_Recordset1['ort']; //unused as of now
$plz = $row_Recordset1['plz'];
$country = 'Germany';
// $street = "Süderstrasse 77";
// $city = "Hamburg";
// $country = "germany";
// $street = $_GET["street"];
// $city = $_GET["city"];
$street = preg_replace('/\s+/', '+', $street);
$city = preg_replace('/\s+/', '+', $city);
$country = preg_replace('/\s+/', '+', $country);
//$street = preg_replace('?', '', $street);
//$city = preg_replace('?', '', $city);
// Your Google Maps API key
$key = 'test';
$address = "http://maps.google.com/maps/geo?q=$street,+$plz,+$country&output=xml&key=$key&oe=utf8&sensor=false";
// Retrieve the URL contents
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $address);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$page=curl_exec($ch);
curl_close($ch);
$array = explode(",", $page);
$rightarray = explode("\n", $page);
$status = substr((trim($rightarray[4])),6,3);
if ($status == "200"){
$latlon = explode(",", $rightarray[13]);
$lon1 = trim($latlon[0]);
$lat = trim($latlon[1]);
$lon = substr($lon1, 20);
echo "<br />lat: $lat<br />Lon: $lon";
}else{
echo "error";
}
|
|
|
|
|
The Following User Says Thank You to paulriedel For This Useful Post:
|
|
06-03-2009, 11:18 PM
|
#4 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
Quote:
Originally Posted by paulriedel
Very dirty but quick and works
|
Instead of doing very crude exploding, etc. you could be a little more clear when parsing out what you want.
For example, you could just use strpos to check for the status element containing "200". You could use string functions (strpos, substr, and the like) or regular expressions to get at the lat-lon coordinates.
Alternatively, depending on your needs, you could just use their CSV result format. I don't have PHP 4 to test against, but here is the idea:
PHP Code:
<?php
$address = 'Süderstrasse 77, 20097, Germany'; $endpoint = 'http://maps.google.com/maps/geo?output=csv&key=test&q='; $request = $endpoint.urlencode($address);
// Grab data using file functions $file = fopen($request, 'r'); $data = fgetcsv($file, 30); fclose($file);
// Parse CSV response $status = (int) $data[0]; $accuracy = (int) $data[1]; $latitude = (float) $data[2]; $longitude = (float) $data[3];
echo '<pre>'; var_dump($data, $status, $accuracy, $latitude, $longitude); echo '</pre>';
?>
|
|
|
|
06-03-2009, 09:29 PM
|
#5 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
Thank you for posting this script! It looks useful. I haven't really dabbled with Google Maps too much myself, but it's something I am considering for a future project.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
06-04-2009, 12:49 AM
|
#6 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
Heh! Off topic, perhaps. I read $endpoint.urlencode($address); as some new fancy code. Only now I've put it into Zend Studio to test do I realise it's concatenation, but without the spaces, which threw me. For a moment I was thinking PHP had added a JS syntax.
You had me confused!
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
06-04-2009, 11:41 AM
|
#7 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
Quote:
Originally Posted by Wildhoney
I was thinking PHP had added a JS syntax.
|
Tsk. I just prefer it that way 
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Hybrid Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|