阿里云免网站登录, 内容分发刷新CDN地址链接
写这个,是每次更新了CDN的内容,都要登录阿里云,然后提交刷新预热,很是麻烦。
本来想找个模版的,结果发现只有wordpress的插件,所以写了个简单的提交,就不用去阿里云了,网站后台直接提交。
<?php
//权限验证
include_once('safety.php');
require 'config.php';
// 上面代码根据自己网站配置,可以把API秘钥放在config.php文件里
// 使用以下命令安装阿里云SDK
// composer require alibabacloud/sdk
// 使用阿里云的SDK,确保先通过Composer安装了阿里云SDK
require '../vendor/autoload.php';
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
// 阿里云账户API密钥
//$accessKeyId = "";
//$accessKeySecret = "";
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$paths = explode("\n", trim($_POST['paths']));
$paths = array_map('trim', $paths); // 去除每个路径两边的空白字符
$objectType = $_POST['flushType'] === 'directory' ? "Directory" : "File";
try {
// 设置一个全局客户端
AlibabaCloud::accessKeyClient($accessKeyId, $accessKeySecret)
->regionId('cn-hangzhou') // 设置默认区域
->asDefaultClient();
foreach ($paths as $path) {
// 构造刷新请求
$result = AlibabaCloud::rpc()
->product('Cdn')
// ->scheme('https') // https | http
->version('2018-05-10')
->action('RefreshObjectCaches')
->method('POST')
->host('cdn.aliyuncs.com')
->options([
'query' => [
'ObjectPath' => $path,
'ObjectType' => $objectType,
],
])
->request();
$responseArray = $result->toArray();
$taskId = $responseArray['RefreshTaskId'] ?? '未知任务ID';
// 输出结果
echo "<pre>刷新阿里云CDN资源提交成功:\n任务ID: {$taskId}\n</pre>";
}
} catch (ClientException $e) {
echo "<pre>客户端错误:" . $e->getErrorMessage() . "</pre>";
} catch (ServerException $e) {
echo "<pre>服务端错误:" . $e->getErrorMessage() . "</pre>";
}
}
?>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<link href="images/style.css" rel="stylesheet" type="text/css">
<title>刷新阿里云CDN</title>
<script>
function togglePathsInput(flushType) {
var pathsInput = document.getElementById('paths');
pathsInput.placeholder = flushType === 'directory' ?
'输入要刷新的目录(每行一个)...' :
'输入要刷新的URLs(每行一个)...';
}
</script>
</head>
<body>
<h1>刷新阿里云CDN资源</h1>
<form method="post">
<label><input type="radio" name="flushType" value="url" onchange="togglePathsInput(this.value)" checked> 刷新URL</label>
<label><input type="radio" name="flushType" value="directory" onchange="togglePathsInput(this.value)"> 刷新目录</label><br>
<label for="paths">输入:</label><br>
<textarea id="paths" name="paths" rows="10" cols="50" placeholder="输入要刷新的URLs(每行一个)..."></textarea><br>
<input type="submit" value="刷新CDN">
</form>
</body>
</html>