文章摘要
<?php
// 本地版本号,可根据实际情况修改
$localVersion = ‘1.0.0’;
// 远程版本与下载链接映射文件的 URL
$versionLinkMapUrl = ‘https://example.com/8792.txt’;
// 最大重试次数
$maxRetries = 3;
// 从远程获取内容
function getRemoteContent($url, $maxRetries) {
$retryCount = 0;
while ($retryCount < $maxRetries) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode === 200) {
curl_close($ch);
return $response;
}
curl_close($ch);
$retryCount++;
sleep(1); // 等待 1 秒后重试
}
return null;
}
// 获取版本与下载链接映射
$versionLinkMapContent = getRemoteContent($versionLinkMapUrl, $maxRetries);
if ($versionLinkMapContent === null) {
echo “无法获取版本与下载链接映射,请检查网络连接或稍后重试。”;
exit;
}
$versionLinkMap = [];
$lines = explode(“\n”, trim($versionLinkMapContent));
foreach ($lines as $line) {
list($version, $link) = explode(‘|’, $line);
$versionLinkMap[$version] = $link;
}
// 筛选出可升级的版本
$upgradableVersions = [];
foreach (array_keys($versionLinkMap) as $version) {
if (version_compare($localVersion, $version, ‘<‘)) {
$upgradableVersions[$version] = $versionLinkMap[$version];
}
}
if (!empty($upgradableVersions)) {
echo “检测到可升级版本:<br>”;
foreach ($upgradableVersions as $version => $link) {
echo “<a href=’$link’>升级到版本 $version</a><br>”;
}
} else {
echo “当前版本已是最新版本 $localVersion。”;
}
?>
使用方法:
远程txt文本中格式如下:
1.1.0|https://example.com/upgrade_1_1_0.zip