开发者

Get country location of an IP with native PHP

Read on before you say this is a duplicate, it's not. (as far as I could see)

I want to get the county code in php from the client.

Yes I know you can do this using external sites or with the likes of "geoip_record_by_name" but I don't want to be dependent on an external site, and I can't install "pear" for php as im using shard Dreamhost hosting.

I thought I could just do something like this:

$output = shell_exec('whois '.$ip.' -H | grep country | awk \'{print $2}\'');
echo "<pre>$output</pre>";

But dreamhost seems to have an old version of whois (4.7.5), so I get this error on allot of IPs:

Unknown 开发者_运维问答AS number or IP network. Please upgrade this program.

So unless someone knows how to get a binary of a newer version of whois onto dreamhost im stuck.

Or is there another way I could get the country code from the client who is loading the page?


Whois is just a client for the whois service, so technically you are still relying on an outside site. For the queries that fail, you could try falling back to another site for the query, such as hostip.info, who happen to have a decent API and seem friendly:

http://api.hostip.info/country.php?ip=4.2.2.2

returns

US

Good luck,

--jed

EDIT: @Mint Here is the link to the API on hostip.info: http://www.hostip.info/use.html


MaxMind provide a free PHP GeoIP country lookup class (there is also a free country+city lookup one).

The bit you want is what is mentioned under "Pure PHP module". This doesn't require you to install anything, or be dependent on them, nor does it need any special PHP modules installed. Just save the GeoIP data file somewhere, then use their provided class to interact with it.


Can you just install a copy of whois into your home directory and pass the full path into shell_exec? That way you're not bound to their upgrade schedule.


An alternative, somewhat extreme solution to your problem would be to:

  1. Download the CSV format version of MaxMind's country database
  2. Strip out the information you don't need from the CSV with a script and ...
  3. ... generate a standard PHP file which contains a data structure containing the IP address as the key and the country code as the value.
  4. Include the resulting file in your usual project files and you now have a completely internal IP => country code lookup table.

The disadvantage is that, regularly, you would need to regenerate the PHP file from the latest version of the database. Also, it's a pretty nasty way of doing it in general and performance might not be the best :)


Consider ipcountryphp (my site, my code, my honour) as it provides a local internet-lifetime freely updated database. It's fast and fully self-contained, pluggable into anything PHP 5.3, SQLite3 and beyond. Very fast seeks and no performance penalties.

Enough with shameless self-promotion, let's get serious:

Relying on querying remote services in real-time to get visitor country can become a major bottleneck for your site's functionality depending on the response speed of the queried server. As a rule of thumb you should never query external services for real-time site functionality (like page loading). Using APIs in the background is great but when you need to query the country of each visitor before the page is rendered, you open yourself up to a world of pain. And do keep in mind you're not the only one abusing free services :)

So queries to 3rd-party services stay in the background while only local functionality that relies on no 3rd-party go into the layers there users interact with. Just my slightly performance paranoid take on this :)

PS: Above mentioned script I wrote has IPv6 support too.


Here is a site with a script i just used. The only problem is that you would probably every now and then need to regenerate IPs by yourself... which might be pain and tahts why everyone is telling you to use external API. But for me that wasnt solution as i was pulling like 50 IPs at once, which means i would probably get banned. So solution was to use my own script or to do saves to DB, but i was again pulling images from external sites. Anyway here is the site i found script on:

http://coding-talk.com/f29/country-flag-script-8882/


Here's a few:

  • http://api.hostip.info/get_html.php?ip=174.31.162.48&position=true
  • http://geoiplookup.net/geoapi.php?output=json&ipaddress=174.31.162.48
  • http://ip-api.com/json/174.31.162.48?callback=yourfunction
  • http://ipinfo.io/174.31.162.48

All return slightly different results.


here is also one of them. just change the IP to the variable:
http://api.codehelper.io/ips/?callback=codehelper_ip_callback&ip=143.3.87.193

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜