IP 주소에서 방문자의 도시, 주 및 국가와 같은 정보를 검색하여 위치에 따라 웹 페이지를 사용자 정의 할 수 있습니다. PHP에서 이것을 할 수있는 좋고 신뢰할 수있는 방법이 있습니까? 클라이언트 쪽 스크립팅에 JavaScript, 서버 쪽 스크립팅에 PHP, 데이터베이스에 MySQL을 사용하고 있습니다.
답변
무료 GeoIP 데이터베이스를 다운로드하고 IP 주소를 로컬로 조회하거나 타사 서비스를 사용하여 원격 조회를 수행 할 수 있습니다. 설정이 필요하지 않으므로 더 간단한 옵션이지만 추가 대기 시간이 발생합니다.
사용할 수있는 타사 서비스 중 하나는 내 http://ipinfo.io 입니다. 호스트 이름, 지리적 위치, 네트워크 소유자 및 추가 정보를 제공합니다. 예 :
$ 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
}
다음은 PHP 예제입니다.
$ip = $_SERVER['REMOTE_ADDR'];
$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}/json"));
echo $details->city; // -> "Mountain View"
클라이언트 쪽에서도 사용할 수 있습니다. 다음은 간단한 jQuery 예제입니다.
$.get("https://ipinfo.io/json", function (response) {
$("#ip").html("IP: " + response.ip);
$("#address").html("Location: " + response.city + ", " + response.region);
$("#details").html(JSON.stringify(response, null, 4));
}, "jsonp");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<h3>Client side IP geolocation using <a href="http://ipinfo.io">ipinfo.io</a></h3>
<hr/>
<div id="ip"></div>
<div id="address"></div>
<hr/>Full response: <pre id="details"></pre>
답변
아무도이 특정 API에 대한 정보를 제공하지 않은 것처럼 게시 할 것이라고 생각했지만 그 이후의 것을 정확하게 반환하고 여러 형식으로 반환 할 수 있습니다 json, xml and csv
.
$location = file_get_contents('http://freegeoip.net/json/'.$_SERVER['REMOTE_ADDR']);
print_r($location);
이렇게하면 원하는 모든 것을 얻을 수 있습니다.
{
"ip": "77.99.179.98",
"country_code": "GB",
"country_name": "United Kingdom",
"region_code": "H9",
"region_name": "London, City of",
"city": "London",
"zipcode": "",
"latitude": 51.5142,
"longitude": -0.0931,
"metro_code": "",
"areacode": ""
}
답변
https://geolocation-db.com 의 서비스를 사용하는 순수 Javascript 예제 JSON 및 JSONP 콜백 솔루션을 제공합니다.
- JSON : https://geolocation-db.com/json
- JSONP 콜백 : https://geolocation-db.com/jsonp
jQuery가 필요하지 않습니다!
<!DOCTYPE html>
<html>
<head>
<title>Geo City Locator by geolocation-db.com</title>
</head>
<body>
<div>Country: <span id="country"></span></div>
<div>State: <span id="state"></span></div>
<div>City: <span id="city"></span></div>
<div>Postal: <span id="postal"></span></div>
<div>Latitude: <span id="latitude"></span></div>
<div>Longitude: <span id="longitude"></span></div>
<div>IP address: <span id="ipv4"></span></div>
</body>
<script>
var country = document.getElementById('country');
var state = document.getElementById('state');
var city = document.getElementById('city');
var postal = document.getElementById('postal');
var latitude = document.getElementById('latitude');
var longitude = document.getElementById('longitude');
var ip = document.getElementById('ipv4');
function callback(data)
{
country.innerHTML = data.country_name;
state.innerHTML = data.state;
city.innerHTML = data.city;
postal.innerHTML = data.postal;
latitude.innerHTML = data.latitude;
longitude.innerHTML = data.longitude;
ip.innerHTML = data.IPv4;
}
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://geoilocation-db.com/json/geoip.php?jsonp=callback';
var h = document.getElementsByTagName('script')[0];
h.parentNode.insertBefore(script, h);
</script>
</html>
답변
Google APIS 사용 :
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script>
contry_code = google.loader.ClientLocation.address.country_code
city = google.loader.ClientLocation.address.city
region = google.loader.ClientLocation.address.region
</script>
답변
http://www.hostip.info/ 와 같은 외부 서비스를 사용해야합니다 . 합니다. Google에서 “geo-ip”을 검색하면 더 많은 결과를 얻을 수 있습니다.
Host-IP API는 HTTP 기반이므로 필요에 따라 PHP 또는 JavaScript로 사용할 수 있습니다.
답변
ipapi.co 의 API를 사용하여 봇을 작성했습니다. 다음에서 IP 주소의 위치를 얻는 방법 1.2.3.4
은 php
다음 과 같습니다.
헤더 설정 :
$opts = array('http'=>array('method'=>"GET", 'header'=>"User-Agent: mybot.v0.7.1"));
$context = stream_context_create($opts);
JSON 응답 받기
echo file_get_contents('https://ipapi.co/1.2.3.4/json/', false, $context);
특정 분야 (국가, 시간대 등)
echo file_get_contents('https://ipapi.co/1.2.3.4/country/', false, $context);
답변
이 질문은 보호되어 있습니다. 그러나 나는 여기에 답을 보지 못합니다. 제가 보는 것은 많은 사람들이 그들이 같은 질문을하면서 나온 것을 보여줍니다.
현재 IP 소유권과 관련된 첫 번째 연락 지점 역할을하는 다양한 기능을 갖춘 5 개의 지역 인터넷 레지스트리가 있습니다. 프로세스가 유동적이기 때문에 다양한 서비스가 때때로 작동하고 다른 시간에는 작동하지 않습니다.
Who Is는 (분명히) 고대 TCP 프로토콜입니다. 원래의 작동 방식은 포트 43에 연결하는 것이기 때문에 임대 연결, 방화벽 등을 통해 라우팅하는 데 문제가 있습니다.
현재 대부분의 Who Is는 RESTful HTTP 및 ARIN을 통해 수행되며 RIPE 및 APNIC는 RESTful 서비스가 작동합니다. LACNIC의 결과는 503을 반환하며 AfriNIC은 분명히 그러한 API를 가지고 있지 않습니다. (모두 온라인 서비스가 있습니다.)
IP의 등록 된 소유자의 주소는 얻을 수 있지만 고객의 위치는 아닙니 다. 주소를 가져와야합니다. 또한 프록시는 발신자라고 생각하는 IP의 유효성을 검사 할 때 걱정할 필요가 없습니다.
사람들은 그들이 추적하고 있다는 개념에 감사하지 않기 때문에-내 생각은-고객으로부터 직접 허락을 얻어 그 개념을 이해하기를 기대합니다.