davieboy Posted September 17, 2011 Share Posted September 17, 2011 HI All im trying to follow the o' tutorials but coming up totally blank if(!$data=simplexml_load_file("http://maps.googleapis.com/maps/api/directions/xml?origin=51.519812,-0.200774&destination=51.26246,-1.084024&sensor=false")){ trigger_error('Error reading XML file',E_USER_ERROR); } echo 'Displaying contents of XML file...<br />'; foreach($data as $directions){ echo 'Start: '.$directions->route->leg->step->start_location->lat.' <br>End: '.$directions->route->leg->step->end_location->lng.'<br> HTML: '.$directions->route->leg->step->html_instructions.' <br>Distance: '.$directions->route->leg->step->distancee->text.'<br /><br>'; } Results just wont show at all, i know its to do with the way im going through the xml fields, but cant see where Can anyone help me at all? Quote Link to comment https://forums.phpfreaks.com/topic/247327-xml-help/ Share on other sites More sharing options...
possien Posted September 17, 2011 Share Posted September 17, 2011 Try this after if(!$data=simplexml_load_file("http://maps.googleapis.com/maps/api/directions/xml?origin=51.519812,-0.200774&destination=51.26246,-1.084024&sensor=false")){ trigger_error('Error reading XML file',E_USER_ERROR);} Use this... $start = $data->route->leg->step->start_location->lat; $end = $data->route->leg->step->end_location->lng; $html = $data->route->leg->step->html_instructions; $distance = $data->route->leg->step->distance->text; echo "Start: ".$start."<br />"; echo "End: ".$end."<br />"; echo "HTML: ".$html."<br />"; echo "Distance: ".$distance."<br />"; Quote Link to comment https://forums.phpfreaks.com/topic/247327-xml-help/#findComment-1270285 Share on other sites More sharing options...
possien Posted September 18, 2011 Share Posted September 18, 2011 Let me add a little more. I think what you are doing is trying to extract all of the "steps" in the XML. Since there are many steps, you are trying to extract each step. Try looking at the PHP example: http://www.php.net/manual/en/simplexmlelement.children.php My example only takes the first child with the name of step. Quote Link to comment https://forums.phpfreaks.com/topic/247327-xml-help/#findComment-1270291 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.