Question

This question already has an answer here:

I want to find country, city, latitude and longitude from IP address using php. I am using this url and it is returning data in xml format.

http://www.ipgp.net/api/xml/122.163.6.58

The data is coming like this:

<IpLookup>
    <Ip>122.163.6.58</Ip>
    <Code>IN</Code>
    <Country>India</Country>
    <Flag>http://www.ipgp.net/flags/in.png</Flag>
    <City>Calcutta</City>
    <Region>West Bengal</Region>
    <Isp></Isp>
    <Lat>22.5697</Lat>
    <Lng>88.3697</Lng>
</IpLookup>

Can anybody suggest how to parse and get the result

Was it helpful?

Solution

OTHER TIPS

Use the XML parser included in PHP?

I've been using this personally: http://ipinfodb.com/

The examples are very clear and concise and the API is very fast.

Good luck.

The use of API in PHP has already described in their website. Why you use but don't read?

http://www.ipgp.net/developer-tools/

I'll suggest you to use xpath this is more easier schema for accessing the attributes. for example for your current data i have the following:

$file = 'file.xml';
$xml = new SimpleXMLElement($file, NULL, TRUE);
$ipz = $xml->xpath("/IpLookup/Ip");
$country = $xml->xpath("/IpLookup/Country/");
foreach($ipz as $ip)
{
    foreach($country as $country)
    {
      echo $ip.'<br/>';
      echo $country.'<br/>';
    }
}

this code will return you the ip and the country for your current xml. you can edit it in your own way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top