Last active
July 23, 2021 12:45
-
-
Save gaffling/b05a328b764c7d2937d6f4dcfbbb77d4 to your computer and use it in GitHub Desktop.
[Geo IP] Get Geo Data of a given IP #php #function #geo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://www.geoplugin.com/examples | |
function ipInfo($ip = NULL, $purpose = "location", $deep_detect = TRUE) { | |
if ( isBot() ) return false; | |
$output = NULL; | |
if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) { | |
$ip = $_SERVER["REMOTE_ADDR"]; | |
if ($deep_detect) { | |
if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP)) | |
$ip = $_SERVER['HTTP_X_FORWARDED_FOR']; | |
if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP)) | |
$ip = $_SERVER['HTTP_CLIENT_IP']; | |
} | |
} | |
$purpose = str_replace(array("name", "\n", "\t", " ", "-", "_"), NULL, strtolower(trim($purpose))); | |
$support = array("land", "land_code", "bundesland", "kontinent", "stadt", "location", "adresse", "plz"); | |
$continents = array( | |
"AF" => "Afrika", | |
"AN" => "Antarctica", | |
"AS" => "Asien", | |
"EU" => "Europa", | |
"OC" => "Australien", | |
"NA" => "Nord Amerika", | |
"SA" => "Süd Amerika" | |
); | |
if (filter_var($ip, FILTER_VALIDATE_IP) && in_array($purpose, $support)) { | |
$ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip)); | |
if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) { | |
switch ($purpose) { | |
case "location": | |
$zip = "http://www.geoplugin.net/extras/postalcode.gp?format=json&lat=".@$ipdat->geoplugin_latitude.'&lon='.@$ipdat->geoplugin_longitude; | |
$zip_code = @json_decode(file_get_contents($zip)); | |
$output = array( | |
"plz" => @$zip_code->geoplugin_postCode, | |
"stadt" => @$ipdat->geoplugin_city, | |
"land" => @$ipdat->geoplugin_countryName='Germany'?'Deutschland':@$ipdat->geoplugin_countryName, | |
); | |
break; | |
case "plz": | |
$zip = "http://www.geoplugin.net/extras/postalcode.gp?format=json&lat=".@$ipdat->geoplugin_latitude.'&lon='.@$ipdat->geoplugin_longitude; | |
$zip_code = @json_decode(file_get_contents($zip)); | |
$output = @$zip_code->geoplugin_postCode; | |
break; | |
case "adresse": | |
$address = array($ipdat->geoplugin_countryName); | |
if (@strlen($ipdat->geoplugin_regionName) >= 1) | |
$address[] = $ipdat->geoplugin_regionName; | |
if (@strlen($ipdat->geoplugin_city) >= 1) | |
$address[] = $ipdat->geoplugin_city; | |
$output = implode(", ", array_reverse($address)); | |
break; | |
case "stadt": | |
$output = @$ipdat->geoplugin_city; | |
break; | |
case "bundesland": | |
$output = @$ipdat->geoplugin_regionName; | |
break; | |
case "kontinent": | |
$output = @$continents[strtoupper($ipdat->geoplugin_continentCode)]; | |
break; | |
case "land": | |
$output = @$ipdat->geoplugin_countryName; | |
break; | |
case "land_code": | |
$output = @$ipdat->geoplugin_countryCode; | |
break; | |
} | |
} | |
} | |
return $output; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://www.zippopotam.us/ | |
function cityFromZip($zip) { | |
if ( isBot() ) return false; | |
// When true, JSON objects will be returned as associative arrays; when false, JSON objects will be returned as objects | |
$data = @json_decode(file_get_contents("http://api.zippopotam.us/de/" . $zip), true); | |
return @$data['places'][0]['place name']; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* ------------------------------------------------------- */ | |
/* [Geo IP] Get Geo Data of a given IP #php #function #geo */ | |
/* ------------------------------------------------------- */ | |
function geoIp($ip=null) { | |
// or use this api it returns xml: http://ipinfodb.com/ip_query.php?ip=80.139.72.178 | |
if ($ip==null) return false; | |
$array = array(); | |
$web_page = file_get_contents( 'http://www.geoiptool.com/en/?IP='.$ip ); | |
preg_match_all('#<div([^>]*)([^>]*)?>(.*)</div>#Us', $web_page, $t_array); | |
for($j=0;$j<count($t_array[0]);$j++) { | |
// parse data | |
$table = $t_array[0][$j]; | |
preg_match_all('#<div class="data-item"([^>]*)?>(.*)</div>#Us', $table, $tr_array); | |
for($i=0;$i<count($tr_array[0]);$i++) { | |
$tar = explode(':', strip_tags ( $tr_array[0][$i] ) ); | |
$array[ trim($tar[0]) ] = trim($tar[1], " )(\n\t"); | |
} | |
} | |
return $array; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment