我需要使用PHP获取通过其IP地址访问我的网站的用户的语言。我该怎么做,有任何API可以做到这一点,请指教
答案 0 :(得分:5)
您可以使用任何geoIP模块。它允许您通过IP检测国家/地区。但这不是很正确的方法。例如我现在在泰国(我有泰国知识产权),但我的语言是俄语:)
当google以泰语显示页面时,这不是很好。
对于检测语言,您可以使用浏览器中的标题。首标中列出了首选语言。
答案 1 :(得分:5)
按照之前的建议使用GeoIP模块,然后在项目的某处插入此代码:
if($country = geoip_country_code_by_name($host))
{
switch($country)
{
case "DJ":
case "ER":
case "ET":
$lang = "aa";
break;
case "AE":
case "BH":
case "DZ":
case "EG":
case "IQ":
case "JO":
case "KW":
case "LB":
case "LY":
case "MA":
case "OM":
case "QA":
case "SA":
case "SD":
case "SY":
case "TN":
case "YE":
$lang = "ar";
break;
case "AZ":
$lang = "az";
break;
case "BY":
$lang = "be";
break;
case "BG":
$lang = "bg";
break;
case "BD":
$lang = "bn";
break;
case "BA":
$lang = "bs";
break;
case "CZ":
$lang = "cs";
break;
case "DK":
$lang = "da";
break;
case "AT":
case "CH":
case "DE":
case "LU":
$lang = "de";
break;
case "MV":
$lang = "dv";
break;
case "BT":
$lang = "dz";
break;
case "GR":
$lang = "el";
break;
case "AG":
case "AI":
case "AQ":
case "AS":
case "AU":
case "BB":
case "BW":
case "CA":
case "GB":
case "IE":
case "KE":
case "NG":
case "NZ":
case "PH":
case "SG":
case "US":
case "ZA":
case "ZM":
case "ZW":
$lang = "en";
break;
case "AD":
case "AR":
case "BO":
case "CL":
case "CO":
case "CR":
case "CU":
case "DO":
case "EC":
case "ES":
case "GT":
case "HN":
case "MX":
case "NI":
case "PA":
case "PE":
case "PR":
case "PY":
case "SV":
case "UY":
case "VE":
$lang = "es";
break;
case "EE":
$lang = "et";
break;
case "IR":
$lang = "fa";
break;
case "FI":
$lang = "fi";
break;
case "FO":
$lang = "fo";
break;
case "BE":
case "FR":
case "SN":
$lang = "fr";
break;
case "IL":
$lang = "he";
break;
case "IN":
$lang = "hi";
break;
case "HR":
$lang = "hr";
break;
case "HT":
$lang = "ht";
break;
case "HU":
$lang = "hu";
break;
case "AM":
$lang = "hy";
break;
case "ID":
$lang = "id";
break;
case "IS":
$lang = "is";
break;
case "IT":
$lang = "it";
break;
case "JP":
$lang = "ja";
break;
case "GE":
$lang = "ka";
break;
case "KZ":
$lang = "kk";
break;
case "GL":
$lang = "kl";
break;
case "KH":
$lang = "km";
break;
case "KR":
$lang = "ko";
break;
case "KG":
$lang = "ky";
break;
case "UG":
$lang = "lg";
break;
case "LA":
$lang = "lo";
break;
case "LT":
$lang = "lt";
break;
case "LV":
$lang = "lv";
break;
case "MG":
$lang = "mg";
break;
case "MK":
$lang = "mk";
break;
case "MN":
$lang = "mn";
break;
case "MY":
$lang = "ms";
break;
case "MT":
$lang = "mt";
break;
case "MM":
$lang = "my";
break;
case "NP":
$lang = "ne";
break;
case "AW":
case "NL":
$lang = "nl";
break;
case "NO":
$lang = "no";
break;
case "PL":
$lang = "pl";
break;
case "AF":
$lang = "ps";
break;
case "AO":
case "BR":
case "PT":
$lang = "pt";
break;
case "RO":
$lang = "ro";
break;
case "RU":
case "UA":
$lang = "ru";
break;
case "RW":
$lang = "rw";
break;
case "AX":
$lang = "se";
break;
case "SK":
$lang = "sk";
break;
case "SI":
$lang = "sl";
break;
case "SO":
$lang = "so";
break;
case "AL":
$lang = "sq";
break;
case "ME":
case "RS":
$lang = "sr";
break;
case "SE":
$lang = "sv";
break;
case "TZ":
$lang = "sw";
break;
case "LK":
$lang = "ta";
break;
case "TJ":
$lang = "tg";
break;
case "TH":
$lang = "th";
break;
case "TM":
$lang = "tk";
break;
case "CY":
case "TR":
$lang = "tr";
break;
case "PK":
$lang = "ur";
break;
case "UZ":
$lang = "uz";
break;
case "VN":
$lang = "vi";
break;
case "CN":
case "HK":
case "TW":
$lang = "zh";
break;
default:break;
}
}
答案 2 :(得分:1)
虽然您可以在IP地址上查找以了解一般物理位置,但物理位置可能与您正在查找其IP地址的人的语言无关。
例如,虽然IP位置可能是美国的纽约地区,但该人可能会说普通话作为他们的主要语言,因为他们正在访问该地区。
以下是可用于IP地址查找http://ipinfodb.com/ip_location_api.php
的服务以下是stackoverflow讨论Get user location by IP address in C#,也可能会有所帮助。
但是,您需要提供一种允许更改语言选择的机制。并使用cookie或类似的东西来记住语言选择。
位置信息最常见的用途是提供有针对性的广告,这种广告更有可能对该位置的人有用。
编辑:使用移动设备
由于通过高速3G和4G蜂窝网络访问互联网的移动设备和智能手机正变得越来越普遍,因此问题在于IP地址地理定位是否适用于这些类型的设备。
简短的回答是,不是很好。请参阅Microsoft Research的这篇文章Where's that Phone?: Geolocating IP Addresses on 3G Networks。
此外,还有stackoverflow question on mobile phone location from a HTTP request。
这是一篇文章Geolocating IP addressesin Cellular Data Networks,它也提供了有关该问题的一些信息。
答案 3 :(得分:0)
function ipToCountry($ip) { $info = file_get_contents("http://who.is/whois-ip/ip-address/$ip"); list($a, $b) = explode('COUNTRY: ', strtoupper($info)); return substr(trim($b),0,2); } function geLangCode() { $countryCode = library::ipToCountry($_SERVER['REMOTE_ADDR']); return $yourQuery }
答案 4 :(得分:0)
您可以尝试我的API,https://ipinfo.io。它默认返回JSON,包含许多不同的字段:
$ curl ipinfo.io
{
"ip": "24.6.61.239",
"hostname": "c-24-6-61-239.hsd1.ca.comcast.net",
"city": "Mountain View",
"region": "California",
"country": "US",
"loc": "37.3845,-122.0881",
"org": "AS7922 Comcast Cable Communications, LLC",
"postal": "94040"
}
有关详细信息,请参阅https://ipinfo.io/developers。
如果您这样做是为了检测语言,那么最好不要查看Accept-Language
标题,这是根据用户的偏好设置的,而不是根据用户的当前位置猜测他们会说什么语言