吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 593|回复: 3
收起左侧

[求助] 如何根据key调用次数达到最高值自动停用key,凌晨12点自动恢复

[复制链接]
skoy03 发表于 2023-11-15 23:19
本帖最后由 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就暂时停用几分钟

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

bianyuan456 发表于 2023-11-16 00:56
思路很清晰呀,你得找个数据库或者文本持续化存储这个key的调用次数,每次调用都判断超过次数了没。
思路说的是伪代码,你要是懒得写试试AI生成吧。
sai609 发表于 2023-11-16 08:01
 楼主| skoy03 发表于 2023-11-16 11:03
bianyuan456 发表于 2023-11-16 00:56
思路很清晰呀,你得找个数据库或者文本持续化存储这个key的调用次数,每次调用都判断超过次数了没。
思路 ...

哪里有AI生成?有地址吗?
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-24 17:40

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表