jasonc Posted September 22, 2016 Share Posted September 22, 2016 Please can someone advise how I correctly access each of the nodes in an XML file. echo($geoLocXML->isp . '<br>'); this is what I used before and it worked up to last month, then all of a sudden it failed. I have also tried this code on a different server and it fails there too, so it suggests there is an error in the code, but for the life of me I can not find it. <?php //exit; error_reporting(-1); //$ip = $_SERVER['REMOTE_ADDR']; //$geoLocXML = getGeoLocXML($ip); //->result[0]; //$geoLocXML = file_get_contents('http://api.geoiplookup.net/?query='.$ip); $getGeoLocXML = simplexml_load_file('http://www.mysite.com/getGeoLocXML.xml'); /* $getGeoLocXML = '<?xml version="1.0" encoding="iso-8859-1"? > <ip> <results><result><ip>123.123.123.123</ip><host>123.123.123.123</host><isp>Provider</isp><city>My City</city><countrycode>GB</countrycode><countryname>United Kingdom</countryname><latitude>123.123</latitude><longitude>-1.123</longitude></result></results> </ip> '; */ $geoLocXML = $getGeoLocXML->result[0]; echo($geoLocXML . '<br>'); echo($geoLocXML->isp . '<br>'); echo($geoLocXML[city] . '<br>'); echo($geoLocXML[countryname] . '<br>'); /* <results> <result> <ip>123.123.123.123</ip> <host>123.123.123.123</host> <isp>Provider</isp> <city>My City</city> <countrycode>GB</countrycode> <countryname>United Kingdom</countryname> <latitude>123.123</latitude> <longitude>-1.123</longitude> </result> </results> */ ?> <?xml version="1.0" encoding="iso-8859-1"?> <ip> <results><result><ip>123.123.123.123</ip><host>123.123.123.123</host><isp>Provider</isp><city>My City</city><countrycode>GB</countrycode><countryname>United Kingdom</countryname><latitude>123.123</latitude><longitude>-1.123</longitude></result></results> </ip> Quote Link to comment Share on other sites More sharing options...
requinix Posted September 23, 2016 Share Posted September 23, 2016 Use -> for nodes and [] for attributes (or indexes). So like the ISP value, and both use the arrow. $geoLocXML = $getGeoLocXML->result[0]; echo($geoLocXML . '<br>'); echo($geoLocXML->isp . '<br>'); echo($geoLocXML->city . '<br>'); echo($geoLocXML->countryname . '<br>'); 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.