문제
특정 IP 주소에 대해 국가를 검색하는 간단한 방법을 아는 사람이 있습니까?ISO_3166-1 형식이 바람직합니까?
해결책
우리 회사를 포함해 많은 사람들이 MaxMind GeoIP를 사용하는 것 같습니다.
무료 버전이 있습니다 지오라이트 유료 버전만큼 정확하지는 않지만 간단한 것을 추구하는 경우에는 충분할 수 있습니다.
다른 팁
두 가지 접근 방식이 있습니다.인터넷 서비스를 사용하고 일종의 로컬 목록(아마도 라이브러리에 싸여 있음)을 사용합니다.당신이 원하는 것은 당신이 무엇을 만들고 있는지에 달려 있습니다.
서비스의 경우:
목록의 경우:
- http://www.maxmind.com/app/geoip_country (언급했듯이 오리온)
RIR에서 목록을 다운로드하여 직접 작성할 수 있습니다.
공개 API를 갖춘 멋진 무료 서비스는 다음과 같습니다.http://www.hostip.info/use.html
ipinfodb IP에 대한 무료 데이터베이스와 API를 국가 간 또는 그 반대로 제공합니다.그들은 MaxMind의 무료 데이터를 사용합니다.데이터는 매달 업데이트되며, 상당한 정확성을 갖춘 훌륭한 무료 대안입니다.
나는 그 hostip.info 사이트가 얼마나 정확한지 모릅니다.방금 그 사이트를 방문했는데, 제 나라가 캐나다라고 나와 있었습니다.저는 미국에 있고 제 사무실에서 사용하는 ISP는 미국에서만 운영됩니다.추측을 수정할 수는 있지만 이 서비스를 사용하여 국가별로 웹사이트 방문자를 추적하는 경우 데이터가 올바른지 알 수 있는 방법이 없습니다.물론 나는 단지 하나의 데이터 포인트일 뿐이다..csv 파일인 GeoLite 국가 데이터베이스를 다운로드했는데 내 IP 주소가 미국으로 올바르게 식별되었습니다.
MaxMind 제품군(유료 또는 무료)의 또 다른 이점은 데이터가 있으므로 다른 시스템에 대한 웹 서비스 호출로 인한 성능 저하가 발생하지 않는다는 것입니다.
가장 정확한 것은 Digital Elements NetAcuity입니다...무료는 아니지만 대부분의 경우 지불한 만큼 얻을 수 있습니다....디지털 요소
제공되는 솔루션을 사용할 수 있습니다. 이 질문.
하지만 2자리 국가 코드가 반환됩니다.
이 PHP 코드를 사용해보십시오
<?php $ip = $_SERVER['REMOTE_ADDR'];
$json = file_get_contents("http://api.easyjquery.com/ips/?ip=".$ip."&full=true");
$json = json_decode($json,true);
$timezone = $json[localTimeZone];?>
내 서비스를 이용하실 수 있습니다. http://ipinfo.io, 이를 위해.API는 IP 주소에 대한 다양한 세부 정보를 반환합니다.
$ curl ipinfo.io/8.8.8.8
{
"ip": "8.8.8.8",
"hostname": "google-public-dns-a.google.com",
"loc": "37.385999999999996,-122.0838",
"org": "AS15169 Google Inc.",
"city": "Mountain View",
"region": "CA",
"country": "US",
"phone": 650
}
국가 코드만 뒤에 오는 경우에는 URL에 /country를 추가하면 됩니다.
$ curl ipinfo.io/8.8.8.8/country
US
사용할 수 있는 일반적인 PHP 함수는 다음과 같습니다.
function ip_details($ip) {
$json = file_get_contents("http://ipinfo.io/{$ip}");
$details = json_decode($json);
return $details;
}
$details = ip_details("8.8.8.8");
echo $details->city; // => Mountain View
echo $details->country; // => US
echo $details->org; // => AS15169 Google Inc.
echo $details->hostname; // => google-public-dns-a.google.com
이 예에서는 IP 8.8.8.8을 사용했지만 사용자 IP에 대한 세부 정보를 원하면 전달하면 됩니다. $_SERVER['REMOTE_ADDR']
대신에.자세한 내용은 다음에서 확인할 수 있습니다. http://ipinfo.io/developers
ipToCountry($ip) 함수를 사용하세요. http://www.mmtutorialvault.com/php-ip-to-country-function/
다음과 같이 이 작업을 수행하는 웹 서비스 API를 사용할 수 있습니다.
see example of service: http://ip-api.com and usage: http://whatmyip.info
보다 ipdata.co IP 주소에서 여러 데이터 포인트를 제공합니다.
API는 매우 빠르며, 각각 매일 8억 개가 넘는 호출을 처리할 수 있는 10개의 글로벌 엔드포인트가 있습니다.
다음은 컬의 예입니다.
curl https://api.ipdata.co/78.8.53.5
{
"ip": "78.8.53.5",
"city": "G\u0142og\u00f3w",
"region": "Lower Silesia",
"region_code": "DS",
"country_name": "Poland",
"country_code": "PL",
"continent_name": "Europe",
"continent_code": "EU",
"latitude": 51.6461,
"longitude": 16.1678,
"asn": "AS12741",
"organisation": "Netia SA",
"postal": "67-200",
"currency": "PLN",
"currency_symbol": "z\u0142",
"calling_code": "48",
"flag": "https://ipdata.co/flags/pl.png",
"emoji_flag": "\ud83c\uddf5\ud83c\uddf1",
"time_zone": "Europe/Warsaw",
"is_eu": true,
"suspicious_factors": {
"is_tor": false
}
}⏎
무료로 사용해 볼 수 있습니다 IP2Location LITE 데이터베이스
MySQL에서 테이블을 생성하려면
CREATE DATABASE ip2location;
USE ip2location;
CREATE TABLE `ip2location_db1`(
`ip_from` INT(10) UNSIGNED,
`ip_to` INT(10) UNSIGNED,
`country_code` CHAR(2),
`country_name` VARCHAR(64),
INDEX `idx_ip_to` (`ip_to`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
데이터를 가져오려면
LOAD DATA LOCAL
INFILE 'IP2LOCATION-LITE-DB1.CSV'
INTO TABLE
`ip2location_db1`
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 0 LINES;
MySQL을 쿼리하는 PHP 코드
<?php
// Replace this MYSQL server variables with actual configuration
$mysql_server = "mysql_server.com";
$mysql_user_name = "UserName";
$mysql_user_pass = "Password";
// Retrieve visitor IP address from server variable REMOTE_ADDR
$ipaddress = $_SERVER["REMOTE_ADDR"];
// Convert IP address to IP number for querying database
$ipno = Dot2LongIP($ipaddress);
// Connect to the database server
$link = mysql_connect($mysql_server, $mysql_user_name, $mysql_user_pass) or die("Could not connect to MySQL database");
// Connect to the IP2Location database
mysql_select_db("ip2location") or die("Could not select database");
// SQL query string to match the recordset that the IP number fall between the valid range
$query = "SELECT * FROM ip2location_db1 WHERE $ipno <= ip_to LIMIT 1";
// Execute SQL query
$result = mysql_query($query) or die("IP2Location Query Failed");
// Retrieve the recordset (only one)
$row = mysql_fetch_object($result);
// Keep the country information into two different variables
$country_code = $row->country_code;
$country_name = $row->country_name;
echo "Country_code: " . $country_code . "<br/>";
echo "Country_name: " . $country_name . "<br />";
// Free recordset and close database connection
mysql_free_result($result);
mysql_close($link);
// Function to convert IP address (xxx.xxx.xxx.xxx) to IP number (0 to 256^4-1)
function Dot2LongIP ($IPaddr) {
if ($IPaddr == "")
{
return 0;
} else {
$ips = explode(".", $IPaddr);
return ($ips[3] + $ips[2] * 256 + $ips[1] * 256 * 256 + $ips[0] * 256 * 256 * 256);
}
}
?>