来源: http://www.phpandstuff.com/articles/geoip-country-lookup-with-php
$country_code
";
// 获取国家名称
$country_name = geoip_country_name_by_addr($gi, $_SERVER['REMOTE_ADDR']);
echo "Your country name is: $country_name
";
// close the database
geoip_close($gi);
//运行结束时间
$endTimes = utime();
$runTimes = sprintf( '%0.4f', ( $endTimes - $startTimes ) );
echo "Processed in " . $runTimes . "second.";
?>
注:在本地测试的话因 为$_SERVER['REMOTE_ADDR']和$_SERVER['REMOTE_ADDR']可能是127.0.0.1,所 以输出的内容为空。可以自己带入IP测试
或者 使用某网站的API
API 1.
1. 返回文字
http://api.hostip.info/get_html.php?ip=12.215.42.19&position=true
2. 返回图片
API 2. (需要申请api key ,免费的,类似google)
城市:
http://api.ipinfodb.com/v3/ip-city/?key=&ip=74.125.45.100
国家(更快) :
http://api.ipinfodb.com/v3/ip-country/?key=&ip=74.125.45.100
Parameter | Required | Default | Value |
---|---|---|---|
key | Yes | API key provided with your free account. | |
ip | No | Client IP | IP address |
format | No | raw | raw, xml, json |
callback | No | Required when using json callback. |
API | Precision | Timezone | Domains lookups |
---|---|---|---|
ip-city | City | Yes | Yes |
ip-country | Country | No | Yes |
使用类:
apiKey = $key;
}
public function getError(){
return implode("\n", $this->errors);
}
public function getCountry($host){
return $this->getResult($host, 'ip-country');
}
public function getCity($host){
return $this->getResult($host, 'ip-city');
}
private function getResult($host, $name){
$ip = @gethostbyname($host);
if(preg_match('/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/', $ip)){
$xml = @file_get_contents('http://' . $this->service . '/' . $this->version . '/' . $name . '/?key=' . $this->apiKey . '&ip=' . $ip . '&format=xml');
try{
$response = @new SimpleXMLElement($xml);
foreach($response as $field=>$value){
$result[(string)$field] = (string)$value;
}
return $result;
}
catch(Exception $e){
$this->errors[] = $e->getMessage();
return;
}
}
$this->errors[] = '"' . $host . '" is not a valid IP address or hostname.';
return;
}
}
?>
include('ip2locationlite.class.php');
//Load the class
$ipLite = new ip2location_lite;
$ipLite->setKey('');
//Get errors and locations
$locations = $ipLite->getCity($_SERVER['REMOTE_ADDR']);
$errors = $ipLite->getError();
//Getting the result
echo "\n";
echo "First result
\n";
if (!empty($locations) && is_array($locations)) {
foreach ($locations as $field => $val) {
echo $field . ' : ' . $val . "
\n";
}
}
echo "
\n";
//Show errors
echo "\n";
echo "Dump of all errors
\n";
if (!empty($errors) && is_array($errors)) {
foreach ($errors as $error) {
echo var_dump($error) . "
\n";
}
} else {
echo "No errors" . "
\n";
}
echo "
\n";
数据库版:
Updated Mar 5 2011
Database | Uncompressed Size(MB) | IP Precision | Data Provided | Format |
---|---|---|---|---|
DB1 | 1.60 | 123.123.123 | ISO country code, country name | CSV BIN |
DB3 | 17.11 | 123.123.123 | ISO country code, country name, state, city | CSV BIN |
DB5 | 21.40 | 123.123.123 | ISO country code, country name, state, city, latitude, longitude | CSV BIN |
DB9 | 22.76 | 123.123.123 | ISO country code, country name, state, city, latitude, longitude, ZIP codes | CSV BIN |
DB11 | 23.28 | 123.123.123 | ISO country code, country name, state, city, latitude, longitude, ZIP codes, time zone | CSV BIN |
key: c9dcc88453e33a9e63ebad8d65f91583e87abd8185dd95f09fbeef6c62264f7d
其他参考
http://pecl.php.net/package/geoip
http://www.geoiptool.com/
http://www.hostip.info/use.html
http://phpweby.com/software/ip2country
http://www.ipinfodb.com/index.php
转帖注明出处:http://justcoding.iteye.com/blog/986355
本站链接: