[php] APEX排行榜爬虫API 2.0
编辑
78
2024-03-10
Apex排行榜爬虫2.0
支持数据库
自定义缓存时间
使用:
保存为php文件,放进web服务器即可,访问地址 http://ip:port/token?token
<?php
// 检查是否有传递 token 参数
if (isset($_GET['token'])) {
$token = $_GET['token'];
// 在这里可以对 token 进行进一步处理
if ($token !== "token") {
http_response_code(401);
die("<h1>无效的令牌</h1>");
}
} else {
http_response_code(401);
die("<h1>非法请求! <br>源码公布在博客,请自行部署 <a href='https://andtun.cn'>地址</a> </h1>");
}
// 缓存时间:30分钟
$cache_time = 1800;
if (file_exists('namelist.json')) {
$timestamp = filemtime('namelist.json');
if (time() - $timestamp > $cache_time) {
fetchAndSaveData();
}
} else {
fetchAndSaveData();
}
$data = json_decode(file_get_contents('namelist.json'), true);
// 返回JSON响应
header('Content-Type: application/json');
echo json_encode($data, JSON_UNESCAPED_UNICODE);
function fetchAndSaveData() {
$servername = "mysql:3306";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
http_response_code(500);
die("连接失败: " . $conn->connect_error);
}
$whitelist = array();
$sql_whitelist = "SELECT id,name FROM whitelist";
$result_whitelist = $conn->query($sql_whitelist);
if ($result_whitelist->num_rows > 0) {
while($row = $result_whitelist->fetch_assoc()) {
$whitelist[] = array(
"id" => (int)$row["id"],
"name" => $row["name"]
);
}
}
$blacklist = array();
$sql_blacklist = "SELECT id,name,reason FROM blacklist";
$result_blacklist = $conn->query($sql_blacklist);
if ($result_blacklist->num_rows > 0) {
while($row = $result_blacklist->fetch_assoc()) {
$blacklist[] = array(
"id" => (int)$row["id"],
"name" => $row["name"]
);
}
}
$conn->close();
function fetchRanklist() {
$ranklist = array();
$url = "https://apexlegendsstatus.com/live-ranked-leaderboards/Battle_Royale/PC";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36");
$response = curl_exec($ch);
curl_close($ch);
if ($response !== false) {
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($response);
libxml_clear_errors();
$xpath = new DOMXPath($dom);
$userModules = $xpath->query('//a[@target="_alsLeaderboard"]');
$rankModules = $xpath->query('//b');
$count = 0;
foreach ($userModules as $index => $userModule) {
$link = $userModule->getAttribute('href');
$userId = substr($link, strrpos($link, '/') + 1);
$username = addslashes($userModule->textContent);
$user = [
"id" => (int)$userId,
"name" => $username,
];
$ranklist[] = $user;
$count++;
if ($count >= 600) {
break;
}
}
}
return $ranklist;
}
$ranklist = fetchRanklist();
$combined_list = array_merge($blacklist, $ranklist);
$unique_list = [];
foreach ($combined_list as $item) {
$unique_list[$item['id']] = $item;
}
$combined_list = array_values($unique_list);
$response = array(
"update" => date("Y-m-d H:i:s"),
"white_list" => $whitelist,
"black_list" => $combined_list
);
file_put_contents('namelist.json', json_encode($response, JSON_UNESCAPED_UNICODE));
}
?>
- 0
- 0
-
赞助
支付宝微信 -
分享