使用原生PHP获取IP的国家位置 [英] Get country location of an IP with native PHP

查看:181
本文介绍了使用原生PHP获取IP的国家位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在你说这是重复的之前阅读,它不是。 (据我所知)



我想从客户端获取php的县代码。



是的我知道你可以使用外部网站或类似的geoip_record_by_name来做到这一点,但我不想依赖于外部网站,并且我不能使用shard Dreamhost托管安装pear作为即时消息。

我想我可以这样做:

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

但是dreamhost似乎有一个旧版本的whois(4.7.5),所以我得到这个错误在所有IP地址上:

 未知的AS号码或IP网络。请升级这个程序。 

所以除非有人知道如何获得更新版本的whois到dreamhost中的二进制文件。 / p>

或者,还有另外一种方法可以从加载页面的客户端获取国家/地区代码吗?

解决方案

Whois只是whois服务的客户端,因此技术上您仍然依赖于外部网站。对于失败的查询,您可以尝试回到查询的其他站点,例如hostip.info,这些站点碰巧拥有体面的API并且看起来很友好:

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

返回

  US 

祝你好运,

< - > jed


编辑: @Mint以下是hostip.info上API的链接: http://www.hostip.info/use .html


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

这篇关于使用原生PHP获取IP的国家位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆