Page 1 of 1
PhP SimpleXMLElement Problem.... help!
Posted: Wed Dec 20, 2006 7:45 am
by Citizen
Here is my code that I use to get the latitude and longitude of a location that works on every query except this one for argentina:
Code: Select all
$locate = " Olavarria, Argentina"
$mapaddress = urlencode("$locate");
$url = "http://maps.google.com/maps/geo?q=$mapaddress&output=xml&key=$key";
$page = file_get_contents($url);
$xml = new SimpleXMLElement($page);
list($longitude, $latitude, $altitude) = explode(",", $xml->Response->Placemark->Point->coordinates);
$alat = $latitude;
$alon = $longitude;
What happens with this query (I have no idea why) is that when it gets to line 5 with the simpleXMLElement line, it kills the page and no code gets put on the page below this line when I view the source.
Any ideas as to how to fix this?
Posted: Wed Dec 20, 2006 8:43 am
by volka
try
Code: Select all
<?php
error_reporting(E_ALL); ini_set('display_errors', true);
if ( !function_exists('simplexml_load_file') ) {
die('simplexml_load_file does not exist. SimpleXML module not installed?');
}
$locate = " Olavarria, Argentina";
$mapaddress = urlencode($locate);
$url = "http://maps.google.com/maps/geo?q=$mapaddress&output=xml&key=$key";
$xml = simplexml_load_file($url);
echo gettype($xml), "<br />\n";
echo gettype($xml->Response), "<br />\n";
echo gettype($xml->Response->Placemark), "<br />\n";
echo gettype($xml->Response->Placemark->Point), "<br />\n";
echo gettype($xml->Response->Placemark->Point->coordinates), "<br />\n";
What does it print?
Posted: Wed Dec 20, 2006 8:49 am
by Citizen
When
$locate="New York";
I get....
object
object
object
object
object
When
$locate="Olavarria, Argentina";
I get
Warning: simplexml_load_file() [function.simplexml-load-file]:
http://maps.google.com/maps/geo?q=Olava ... 9LYIDC3Q:1: parser error : Input is not proper UTF-8, indicate encoding ! Bytes: 0xED 0x61 0x2C 0x20 in /home/gamerbio/public_html/test.php on line 13
Warning: simplexml_load_file() [function.simplexml-load-file]: s><code>200</code><request>geocode</request></Status><Placemark><address>Olavarr in /home/gamerbio/public_html/test.php on line 13
Warning: simplexml_load_file() [function.simplexml-load-file]: ^ in /home/gamerbio/public_html/test.php on line 13
boolean
Notice: Trying to get property of non-object in /home/gamerbio/public_html/test.php on line 16
NULL
Notice: Trying to get property of non-object in /home/gamerbio/public_html/test.php on line 17
Notice: Trying to get property of non-object in /home/gamerbio/public_html/test.php on line 17
NULL
Notice: Trying to get property of non-object in /home/gamerbio/public_html/test.php on line 18
Notice: Trying to get property of non-object in /home/gamerbio/public_html/test.php on line 18
Notice: Trying to get property of non-object in /home/gamerbio/public_html/test.php on line 18
NULL
Notice: Trying to get property of non-object in /home/gamerbio/public_html/test.php on line 19
Notice: Trying to get property of non-object in /home/gamerbio/public_html/test.php on line 19
Notice: Trying to get property of non-object in /home/gamerbio/public_html/test.php on line 19
Notice: Trying to get property of non-object in /home/gamerbio/public_html/test.php on line 19
NULL
Posted: Wed Dec 20, 2006 9:31 am
by volka
Strange, I get
as response from google. It's parsed without errors by simplexml and the script prints
object<br />
object<br />
object<br />
NULL<br />
Notice: Trying to get property of non-object in test.php on line 16
NULL<br />
Posted: Wed Dec 20, 2006 10:26 am
by Citizen
What should I do to avoid this error?
Posted: Wed Dec 20, 2006 5:24 pm
by volka
Let's take a closer look at google's response you get. What's the output of
Code: Select all
$locate = " Olavarria, Argentina";
$mapaddress = urlencode("$locate");
$url = "http://maps.google.com/maps/geo?q=$mapaddress&output=xml&key=$key";
$page = file_get_contents($url);
echo htmlentities($page);
echo "\n---\n";
for($i=0; $i<strlen($page); $i++) {
printf("%02X ", ord($page[$i]));
}
?
Posted: Wed Dec 20, 2006 5:35 pm
by Citizen
This is what that returned:
Code: Select all
<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://earth.google.com/kml/2.0"><Response><name> Olavarria, Argentina</name><Status><code>200</code><request>geocode</request></Status><Placemark><address>Olavarría, Argentina</address><AddressDetails Accuracy="4" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>AR</CountryNameCode><Locality><LocalityName>Olavarría</LocalityName></Locality></Country></AddressDetails><Point><coordinates>-60.330001,-36.900001,0</coordinates></Point></Placemark></Response></kml> --- 3C 3F 78 6D 6C 20 76 65 72 73 69 6F 6E 3D 22 31 2E 30 22 20 65 6E 63 6F 64 69 6E 67 3D 22 55 54 46 2D 38 22 3F 3E 3C 6B 6D 6C 20 78 6D 6C 6E 73 3D 22 68 74 74 70 3A 2F 2F 65 61 72 74 68 2E 67 6F 6F 67 6C 65 2E 63 6F 6D 2F 6B 6D 6C 2F 32 2E 30 22 3E 3C 52 65 73 70 6F 6E 73 65 3E 3C 6E 61 6D 65 3E 20 4F 6C 61 76 61 72 72 69 61 2C 20 41 72 67 65 6E 74 69 6E 61 3C 2F 6E 61 6D 65 3E 3C 53 74 61 74 75 73 3E 3C 63 6F 64 65 3E 32 30 30 3C 2F 63 6F 64 65 3E 3C 72 65 71 75 65 73 74 3E 67 65 6F 63 6F 64 65 3C 2F 72 65 71 75 65 73 74 3E 3C 2F 53 74 61 74 75 73 3E 3C 50 6C 61 63 65 6D 61 72 6B 3E 3C 61 64 64 72 65 73 73 3E 4F 6C 61 76 61 72 72 ED 61 2C 20 41 72 67 65 6E 74 69 6E 61 3C 2F 61 64 64 72 65 73 73 3E 3C 41 64 64 72 65 73 73 44 65 74 61 69 6C 73 20 41 63 63 75 72 61 63 79 3D 22 34 22 20 78 6D 6C 6E 73 3D 22 75 72 6E 3A 6F 61 73 69 73 3A 6E 61 6D 65 73 3A 74 63 3A 63 69 71 3A 78 73 64 73 63 68 65 6D 61 3A 78 41 4C 3A 32 2E 30 22 3E 3C 43 6F 75 6E 74 72 79 3E 3C 43 6F 75 6E 74 72 79 4E 61 6D 65 43 6F 64 65 3E 41 52 3C 2F 43 6F 75 6E 74 72 79 4E 61 6D 65 43 6F 64 65 3E 3C 4C 6F 63 61 6C 69 74 79 3E 3C 4C 6F 63 61 6C 69 74 79 4E 61 6D 65 3E 4F 6C 61 76 61 72 72 ED 61 3C 2F 4C 6F 63 61 6C 69 74 79 4E 61 6D 65 3E 3C 2F 4C 6F 63 61 6C 69 74 79 3E 3C 2F 43 6F 75 6E 74 72 79 3E 3C 2F 41 64 64 72 65 73 73 44 65 74 61 69 6C 73 3E 3C 50 6F 69 6E 74 3E 3C 63 6F 6F 72 64 69 6E 61 74 65 73 3E 2D 36 30 2E 33 33 30 30 30 31 2C 2D 33 36 2E 39 30 30 30 30 31 2C 30 3C 2F 63 6F 6F 72 64 69 6E 61 74 65 73 3E 3C 2F 50 6F 69 6E 74 3E 3C 2F 50 6C 61 63 65 6D 61 72 6B 3E 3C 2F 52 65 73 70 6F 6E 73 65 3E 3C 2F 6B 6D 6C 3E
Posted: Wed Dec 20, 2006 7:35 pm
by volka
The google response should be utf-8 encoded but isn't.
It's about the í in Olavarría (í not i). In utf-8 it should be represented by two bytes:0xC3 0xAD (not 0xED as in latin-1).
work-around/hack:
Code: Select all
$page = file_get_contents($url);
$page = str_replace('<?xml version="1.0" encoding="UTF-8"?>', '<?xml version="1.0" encoding="ISO-8859-1"?>', $page);
$xml = simplexml_load_string($page);
var_dump(explode(",", $xml->Response->Placemark->Point->coordinates));