看看http://www.geoplugin.com/webservices/php並使用緩存來存儲IP地址,因爲每次都不發出請求。
<?php
error_reporting(-1);
ini_set("display_errors", "On");
$ch = curl_init('http://www.geoplugin.net/php.gp?ip={the IP address, used mine for example}');
if (!$ch) {
die('Failed CURL');
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$serverResponse = curl_exec($ch);
if (!$serverResponse) {
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
throw new Exception('HTTP error: ' . $code);
}
die(print_r($serverResponse));
會導致:
Array
(
[geoplugin_request] => My IP
[geoplugin_status] => 206
[geoplugin_credit] => Some of the returned data includes GeoLite data created by MaxMind, available from <a href=\'http://www.maxmind.com\'>http://www.maxmind.com</a>.
[geoplugin_city] =>
[geoplugin_region] =>
[geoplugin_areaCode] => 0
[geoplugin_dmaCode] => 0
[geoplugin_countryCode] => GB
[geoplugin_countryName] => United Kingdom
[geoplugin_continentCode] => EU
[geoplugin_latitude] => 51.5
[geoplugin_longitude] => -0.13
[geoplugin_regionCode] =>
[geoplugin_regionName] =>
[geoplugin_currencyCode] => GBP
[geoplugin_currencySymbol] => £
[geoplugin_currencySymbol_UTF8] => £
[geoplugin_currencyConverter] => 0.6003
)
因此,使用國家代碼。
你有IP位置的數據庫嗎? – user1844933
@ user1844933不,我不知道。爲什麼這需要? – egr103