-2

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

gevorg
  • 4,835
  • 4
  • 35
  • 52
Deepak Ranjan Jena
  • 437
  • 5
  • 12
  • 23

5 Answers5

2

Use the XML parser included in PHP?

Core Xii
  • 6,270
  • 4
  • 31
  • 42
0

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.

gevorg
  • 4,835
  • 4
  • 35
  • 52
AliMohsin
  • 329
  • 1
  • 5
  • 10
0

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

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

Good luck.

switz
  • 24,384
  • 25
  • 76
  • 101
0

Use simplexml_load_string().

alex
  • 479,566
  • 201
  • 878
  • 984
0

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/

Wayne
  • 681
  • 1
  • 6
  • 14