私が開発しているウェブサイトの場合、各国から何人のユーザーが私のサイトにアクセスしたかを知りたいのですが、興味があるのは、ユーザーの国を取得する方法です (IP から、多分?)? API を何時間も探し回ったのですが、適切なものが見つかりませんでした。誰かお勧めはありますか?助けてくれてありがとう :)。
9482 次
3 に答える
11
geoplugin.netのような外部 API を使用できます。
$xml = simplexml_load_file("http://www.geoplugin.net/xml.gp?ip=76.109.14.196");
echo $xml->geoplugin_countryName ;
出力国
United States
完全な XML 応答
<geoPlugin>
<geoplugin_request>76.109.14.196</geoplugin_request>
<geoplugin_status>200</geoplugin_status>
<geoplugin_city>West Palm Beach</geoplugin_city>
<geoplugin_region>FL</geoplugin_region>
<geoplugin_areaCode>561</geoplugin_areaCode>
<geoplugin_dmaCode>548</geoplugin_dmaCode>
<geoplugin_countryCode>US</geoplugin_countryCode>
<geoplugin_countryName>United States</geoplugin_countryName>
<geoplugin_continentCode>NA</geoplugin_continentCode>
<geoplugin_latitude>26.761600494385</geoplugin_latitude>
<geoplugin_longitude>-80.091598510742</geoplugin_longitude>
<geoplugin_regionCode>FL</geoplugin_regionCode>
<geoplugin_regionName>Florida</geoplugin_regionName>
<geoplugin_currencyCode>USD</geoplugin_currencyCode>
<geoplugin_currencySymbol>$</geoplugin_currencySymbol>
<geoplugin_currencyConverter>1</geoplugin_currencyConverter>
</geoPlugin>
IPを取得するためのシンプルな機能
function getIP() {
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (! empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
于 2012-10-19T00:41:33.637 に答える
1
基本的なアプローチは、着信 IP アドレス ( にあります$_SERVER['REMOTE_ADDR']
) を記録し、地理位置情報データベースを使用してそれを国情報に変換することです。
地理位置情報データベースを最新の状態に保つ必要があります。覚えておいてください。
地理位置情報データベースを提供する場所がいくつかあります。多少の費用はかかりますが(より正確で最新の傾向があります)、無料のものも通常かなり優れています.
于 2012-10-19T00:41:50.677 に答える