本帖最后由 skoy03 于 2023-11-15 23:23 编辑
[PHP] 纯文本查看 复制代码 <?php
header('Content-Type:application/json');
header('Access-Control-Allow-Origin:*');
header("Access-Control-Allow-Methods:GET,POST");
$ip = gethostbyname($_REQUEST["ip"]);
$ips = $_SERVER['HTTP_X_FORWARDED_FOR'] ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
if ($ip == null) {
if (isset($ips)) {
$ip = explode(',', $ips);
$ip = trim(current($ip));
}
}
$API_key = 'OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77';//腾讯地图开放平台公开key,现被限制qps
$checkUrl = get_headers('https://apis.map.qq.com/ws/location/v1/ip?key=' . $API_key);
$headerStr = json_encode($checkUrl);
$string = substr($headerStr, strripos($headerStr, "X-Limit"));
preg_match_all('/(current_qps|limit_qps|current_pv|limit_pv)=([0-9]+)/', $string, $matches);
$result = array_combine($matches[1], $matches[2]);
$current_qps = $result['current_qps'];当前qps
$limit_qps = $result['limit_qps'];//上限5 qps
$current_pv = $result['current_pv'];当前调用次数
$limit_pv = $result['limit_pv'];//上限10000次
$form = YunTu($API_key,$ip);
$from = json_decode($form,true);
$status = $from['status'];
$message = $from['message'];
$ipe = $from['result']['ip'];
$lat = $from['result']['location']['lat'];
$lng = $from['result']['location']['lng'];
$nation = $from['result']['ad_info']['nation'];
$province = $from['result']['ad_info']['province'];
$city = $from['result']['ad_info']['city'];
$adcode = $from['result']['ad_info']['adcode'];
if ($status == 0) {
$res = array(
'code' => 200,
'msg' => 'success',
'ip' => $ipe,
'nation' => $nation,
'province' => $province,
'city' => $city,
'adcode' => $adcode,
'lat' => $lat,
'lng' => $lng
);
} else {
$res = array(
'code' => 201,
'msg' => $message
);
}
echo json_encode($res, 448);
function YunTu($API_key,$ip)
{
$url = 'https://apis.map.qq.com/ws/location/v1/ip?key=' . $API_key . '&ip=' . $ip;
$ch = curl_init();
$timeout = 30;
$ua= $_SERVER['HTTP_USER_AGENT'];
//$ipv = getIp();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
//curl_setopt($ch, CURLOPT_REFERER,'https://lbs.qq.com/service/webService/webServiceGuide/webServiceIp');
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-FORWARDED-FOR:'.$ipv, 'CLIENT-IP:'.$ipv));
curl_setopt($ch, CURLOPT_USERAGENT, $ua);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
以上代码应该如何修改才能实现当key调用次数达到每日上限就停用这个key调用,凌晨12点自动恢复
假如这个key出现状态码{"status":120}或者current_qps>5就暂时停用几分钟 |