본문 바로가기

PHP

API : IP Geolocation

내 웹페이지 방문자의 IP주소, 국가 정보 등이 궁금해져서 뭐 없나 찾다가 발견했다.

https://ipinfodb.com/api

 

Free IP Geolocation API for IP Address Lookup | IPInfoDB

Free Geolocation tools and APIs for country, region, city and time zone lookup by IP address. Supported both IPv4 and IPv6 address.

ipinfodb.com

위 싸이트에 가입해서 API Key를 얻는다.

싸이트에 들어가면 참고할 예제가 언어별로 있다. 나는 제공된 php 예제로 값을 못 얻었다. 내가 뭐를 잘못한 건지는 모르겠다. 아래처럼 하면 됨. IP주소, 국가, 도시, 동, 우편번호, 위도, 경도가 나온다.

$apiKey = "내꺼";
$url = "http://api.ipinfodb.com/v3/ip-city/?key=".$apiKey."&ip=".$_SERVER['REMOTE_ADDR'];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$api = curl_exec($ch);
curl_close($ch);

echo $api;
exit;

 

728x90