bravo14 Posted June 24, 2020 Share Posted June 24, 2020 Hi all I am trying to get the lat and long properties from the googlemaps geocoding api. $xml=simplexml_load_file($url); if ($xml === false) { echo "Failed loading XML\n"; foreach(libxml_get_errors() as $error) { echo "\t", $error->message; } } else{ $path = $xml->xpath( "/GeocodeResponse/result/geometry/location"); $lat = $path->lat; $lng = $path->lng; echo $lat; echo $lng; $sql1 = "UPDATE tbl_clubs set lat='$lat',lng='$lng' where club_id =$id"; echo $sql1; $result1 = mysqli_query($dbConn,$sql1) or die($mysqli_error($dbConn)); } This is the XML I am using <GeocodeResponse> <status>OK</status> <result> <type>establishment</type> <type>point_of_interest</type> <type>stadium</type> <formatted_address>Aldridge Rd, Perry Barr, Birmingham B42 2ET, UK</formatted_address> <address_component> </address_component> <address_component> </address_component> <address_component> </address_component> <address_component> <long_name>West Midlands</long_name> <short_name>West Midlands</short_name> <type>administrative_area_level_2</type> <type>political</type> </address_component> <address_component> <long_name>England</long_name> <short_name>England</short_name> <type>administrative_area_level_1</type> <type>political</type> </address_component> <address_component> <long_name>United Kingdom</long_name> <short_name>GB</short_name> <type>country</type> <type>political</type> </address_component> <address_component> <long_name>B42 2ET</long_name> <short_name>B42 2ET</short_name> <type>postal_code</type> </address_component> <geometry> <location> <lat>52.5196698</lat> <lng>-1.8986236</lng> </location> <location_type>GEOMETRIC_CENTER</location_type> <viewport> <southwest> <lat>52.5183208</lat> <lng>-1.8999726</lng> </southwest> <northeast> <lat>52.5210188</lat> <lng>-1.8972746</lng> </northeast> </viewport> </geometry> But I am getting the following response Notice: Trying to get property of non-object in /home/sites/1a/9/95f15f28a6/public_html/results/getLatLng.php on line 22 I am guessing my path is wrong, any guidance would be great Quote Link to comment Share on other sites More sharing options...
Zane Posted June 25, 2020 Share Posted June 25, 2020 Where is Line 22? Quote Link to comment Share on other sites More sharing options...
Zane Posted June 25, 2020 Share Posted June 25, 2020 Quote simplexml_load_file->xpath "returns an array of SimpleXMLElement objects" https://www.php.net/manual/en/function.simplexml-load-file.php So using the syntax $path->name will not work and will throw such an error $simplexml_load_file returns one SimpleXMLElement But why even use xpath()? You could just do this. <?php $string = <<<XML <?xml version='1.0'?> <GoogleCodeBase> <result> <geometry> <location> <lat>LATITUDE</lat> <lng>LONGITUDE</lng> </location> </geometry> </result> <result> <geometry> <location> <lat>2nd LATITUDE</lat> <lng>2nd LONGITUDE</lng> </location> </geometry> </result> </GoogleCodeBase> XML; $xml = simplexml_load_string($string); $o = $xml->result->geometry->location->lat; echo $o; /* If you wanted the second result instead, you'd treat it like an array */ $xml = simplexml_load_string($string); $o = $xml->result[1]->geometry->location->lat; echo $o; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.