38 lines
1.4 KiB
PHP
38 lines
1.4 KiB
PHP
<?php
|
|
header("Content-type:application/json;charset=utf-8");
|
|
function is_url_alive(){
|
|
$opts = array(
|
|
'http'=>array(
|
|
'timeout'=>3,
|
|
)
|
|
);
|
|
$context = stream_context_create($opts);
|
|
return @file_get_contents("http://whois.pconline.com.cn/ipJson.jsp", false, $context);
|
|
}
|
|
if(empty($_GET['ip'])){
|
|
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
|
|
}else{
|
|
$ip=$_GET['ip'];
|
|
}
|
|
$db=mysqli_connect("database.ghink.net","ghinkapi","ghink2014","ghinkapi");
|
|
mysqli_query($db,"set names utf8");
|
|
$result=mysqli_query($db,"SELECT `ip` FROM `ip_area` WHERE `ip`='".$ip."'");
|
|
if(empty(mysqli_fetch_array($result))){
|
|
if(is_url_alive()){
|
|
$ipinfo=@file_get_contents("http://whois.pconline.com.cn/ipJson.jsp?ip=".$ip."&json=true");
|
|
$ipinfo=iconv("GB2312","UTF-8",$ipinfo);
|
|
$ipinfo=json_decode($ipinfo);
|
|
foreach($ipinfo as $ii=>$ii2){
|
|
$$ii = $ii2;
|
|
}
|
|
echo '{"ip":"'.$ip.'","pro":"'.$pro.'","city":"'.$city.'","addr":"'.trim($addr).'"}';
|
|
mysqli_query($db,"INSERT INTO `ip_area` (`ip`, `pro`, `city`, `addr`, `last_update`) VALUES ('".$ip."', '".$pro."', '".$city."', '".trim($addr)."', '".time()."')");
|
|
}else{
|
|
echo '{"error":"10002"}';
|
|
}
|
|
}else{
|
|
$result=mysqli_query($db,"SELECT * FROM `ip_area` WHERE `ip`='".$ip."'");
|
|
$result=mysqli_fetch_array($result);
|
|
echo '{"ip":"'.$result['ip'].'","pro":"'.$result['pro'].'","city":"'.$result['city'].'","addr":"'.$result['addr'].'"}';
|
|
}
|
|
mysqli_close($db); |