0

I'm creating a script that will take users location from their ip. I saw this api.But know idea how i can get them in array and print them.

Here is a sample link: http://ipinfodb.com/ip_query.php?ip=74.125.45.100&timezone=true

This a sample respones

<Response>
<Ip>74.125.45.100</Ip>
<Status>OK</Status>
<CountryCode>US</CountryCode>
<CountryName>United States</CountryName>
<RegionCode>06</RegionCode>
<RegionName>California</RegionName>
<City>Mountain View</City>
<ZipPostalCode>94043</ZipPostalCode>
<Latitude>37.4192</Latitude>
<Longitude>-122.057</Longitude>
<TimezoneName>America/Los_Angeles</TimezoneName>
<Gmtoffset>-25200</Gmtoffset>
<Isdst>1</Isdst>
</Response>

How do i get the information from this in an array and print?

esafwan
  • 17,311
  • 33
  • 107
  • 166
  • possible duplicate of [Best XML Parser for PHP](http://stackoverflow.com/questions/188414/best-xml-parser-for-php) and [Parsing XML into Array](http://stackoverflow.com/questions/3428908/parsing-xml-into-array) – Gordon Oct 25 '10 at 09:34

3 Answers3

1

You should look into simpleXML (http://php.net/manual/en/book.simplexml.php)

Using simpleXML you could do something like:

$xml = simplexml_load_file($xmlFile,'SimpleXMLElement', LIBXML_NOCDATA);
$ip = (string) $xml->Ip;
$status = (string) $xml->Status;
Coin_op
  • 10,568
  • 4
  • 35
  • 46
1

Use a SimpleXml instance and loop through all the children nodes

$url = 'http://ipinfodb.com/ip_query.php?ip=74.125.45.100&timezone=true';
$xml = new SimpleXmlElement($url, null, true);

$info = array();
foreach ($xml->children() as $child) {
    $name = $child->getName();
    $info[$name] = $xml->$name;
}

// info['Ip'] = '74.125.45.100', etc
Jurian Sluiman
  • 13,498
  • 3
  • 67
  • 99
0

Look at SimpleXML, it's the easiest way to parse XML with PHP.

jyggen
  • 31
  • 1
  • 6
  • IMO, the "simple" in SimpleXML does not refer to it's purported oh so "simple" usage, but to the complexity of the XML it should be used on, e.g. use SimpleXML when your XML is simple. Use DOM otherwise. But since learning two DOM based libraries is a waste of time, you can just as well learn DOM directly (or XMLReader if you want a pull parser instead) – Gordon Oct 25 '10 at 09:36