0

I want to find the country code of my site visitor using the ipinfodb API.

When I try the following, http://api.ipinfodb.com/v3/ip-country/?key=<My_API_Key>&ip=<Some_IP>, it gives the following output:

OK;;<Some_IP>;US;UNITED STATES

How do I filter this output so that it shows only the country code ?

Regards, Timothy

EDIT:

In reply to Charles,

After searching on google I came to know that the API can be given a 'format attribute as XML' so the following works.

$xml = simplexml_load_file('http://api.ipinfodb.com/v3/ip-country/?key=<My_API>&format=xml&ip=<Some_IP>);

echo $xml->countryCode;

How can I get the same output without the XML argument ?

Thanks

  • Thank for the reply. I have got the desired output with the XML argument. But how can I get it without the extra argument ? (New code has been added to my post) – Timothy Black Dec 15 '12 at 17:09
  • Its easiert when you write the XML that you get from the API. Then we can help you. With your example on top we cannot run your API call. – René Höhle Dec 15 '12 at 17:11

2 Answers2

0
OK;;<Some_IP>;US;UNITED STATES

How do I filter this output so that it shows only the country code ?

I find it curious that you'd be able to invoke SimpleXML, but didn't think of explode, which will turn a string in into an array, splitting on the given delimiter. In this case, we need to explode on ;:

$string_data = 'OK;;127.0.0.1;US;UNITED STATES';
$sploded_data = explode(';', $string_data);
print_r($sploded_data);
echo "\nCountry code: ", $sploded_data[3], "\n";

This should emit:

Array
(
    [0] => OK
    [1] =>
    [2] => 127.0.0.1
    [3] => US
    [4] => UNITED STATES
)
Country code: US

You may wish to review the rest of the string manipulation functions to see if there's any other interesting things you may have missed.

Charles
  • 50,943
  • 13
  • 104
  • 142
0

The above answer isn't particularly helpful, because the server output is only static if you explicitly specify the same IP address every time. If you hardcode a location request for a particular IP address, it's going to the be the same every time. What's the point?

Do this instead

  • Install the PHP class for their API. PHP Class
  • Play around with the PHP sample found here; save it as its own webpage and observe the results. Let's call it "userlocation.php." Please note that the fields will be null if you load from localhost.

Okay, the trick to parse this output is array_values(). Took me forever to figure this, but eventually I stumbled upon this.

So...

$locations = array_values($locations);
echo $locations[n],"<br />\n";
echo $locations[n+1],"<br />\n";
etc.

Whatever element you need, you can get in this way -- and it's dynamic. The code will return whatever country pertains to the user's IP address.

One last note. Take care to paste your API key into the class file and not into userlocation.php. The class file variables are protected, which is a good thing.

Anyway, I'm no expert; just thought I'd share what I've learned.

Community
  • 1
  • 1