65 lines
1.7 KiB
PHP
65 lines
1.7 KiB
PHP
|
|
<?php
|
|||
|
|
error_reporting(0);
|
|||
|
|
date_default_timezone_set('PRC');
|
|||
|
|
header('Access-Control-Allow-Origin: *');
|
|||
|
|
header('Content-type: application/json');
|
|||
|
|
// 应用API KEY
|
|||
|
|
const API_KEY = "";
|
|||
|
|
function curl_get($url){
|
|||
|
|
$header = array(
|
|||
|
|
'Accept: application/json',
|
|||
|
|
);
|
|||
|
|
$curl = curl_init();
|
|||
|
|
curl_setopt($curl, CURLOPT_URL, $url);
|
|||
|
|
curl_setopt($curl, CURLOPT_HEADER, 0);
|
|||
|
|
curl_setopt($curl, CURLOPT_TIMEOUT, 1);
|
|||
|
|
curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);
|
|||
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
|
|||
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
|||
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
|||
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
|
|||
|
|
$data = curl_exec($curl);
|
|||
|
|
|
|||
|
|
// 显示错误信息
|
|||
|
|
if (curl_error($curl)) {
|
|||
|
|
return "Error: " . curl_error($curl);
|
|||
|
|
} else {
|
|||
|
|
// 打印返回的内容
|
|||
|
|
return ($data);
|
|||
|
|
}
|
|||
|
|
curl_close($curl);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function is_domain($domain){
|
|||
|
|
$pat = "/^(\w+:\/\/)?([^\/]+)/i";
|
|||
|
|
if(preg_match($pat, $domain, $matches)){
|
|||
|
|
if($matches[2]){
|
|||
|
|
return 1;
|
|||
|
|
}else{
|
|||
|
|
return 0;
|
|||
|
|
}
|
|||
|
|
}else{
|
|||
|
|
return 0;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
$ip = isset($_GET["ip"])?$_GET["ip"]:'';
|
|||
|
|
$type = 4;
|
|||
|
|
if(!filter_var($ip, FILTER_VALIDATE_IP)){
|
|||
|
|
//非合法IP则判断是否是域名
|
|||
|
|
if(is_domain($ip)){
|
|||
|
|
$ip = gethostbyname($ip); //是域名则获取该域名的IP
|
|||
|
|
}else{
|
|||
|
|
//既非IP也非域名,此处做错误任务处理
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if(filter_var($ip,FILTER_VALIDATE_IP,FILTER_FLAG_IPV4)){
|
|||
|
|
// ip4
|
|||
|
|
$type = 4;
|
|||
|
|
} else if(filter_var($ip,FILTER_VALIDATE_IP,FILTER_FLAG_IPV6)){
|
|||
|
|
//ip6
|
|||
|
|
$type = 6;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
$url = sprintf("https://restapi.amap.com/v5/ip?key=%s&type=%s&ip=%s",API_KEY,$type, urlencode($ip));
|
|||
|
|
echo curl_get($url);
|