views:

22

answers:

3

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?

A: 

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

jyggen
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
+1  A: 

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;
laurencek
+1  A: 

Use a SimpleXml instance and loop through all the children nodes

$url = 'http://ipinfodb.com/ip_query.php?ip=74.125.45.100&amp;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
worked wonderfully... thanx
esafwan