腾讯云 内容分发 刷新CDN地址链接 php

腾讯云免网站登录, 内容分发刷新CDN地址链接

写这个,是每次更新了CDN的内容,都要登录腾讯云,然后提交刷新预热,很是麻烦。

本来想找个模版的,结果发现只有wordpress的插件,所以写了个简单的提交,就不用去腾讯云了,网站后台直接提交。

<?php
// 后台权限验证
include_once('safety.php');
require 'config.php';
// 上面的根据自己的网站环境配置。

// 建议中国大陆地区的用户设置腾讯云镜像源,执行以下命令:
// composer config -g repos.packagist composer https://mirrors.tencent.com/composer/
// 使用以下命令安装SDK
// composer require tencentcloud/tencentcloud-sdk-php
// 如果只想安装某个产品的 SDK,例如CDN服务,可以使用以下命令:
// composer require tencentcloud/cdn

// 使用腾讯云的SDK,确保先通过Composer安装了腾讯云SDK
require '../vendor/autoload.php';

use TencentCloud\Cdn\V20180606\CdnClient;
use TencentCloud\Cdn\V20180606\Models\PurgePathCacheRequest;
use TencentCloud\Common\Credential;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;

// 腾讯云账户API密钥
$secretId = "";
$secretKey = "";


if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $paths = explode("\n", trim($_POST['paths']));
    $paths = array_map('trim', $paths); // 去除每个路径两边的空白字符
    $flushType = $_POST['flushType'] === 'directory' ? "flush" : "delete";

    try {
		// 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey
        $cred = new Credential($secretId, $secretKey);
        $httpProfile = new HttpProfile();
        $httpProfile->setEndpoint("cdn.tencentcloudapi.com");

        $clientProfile = new ClientProfile();
        $clientProfile->setHttpProfile($httpProfile);
        $client = new CdnClient($cred, "", $clientProfile);

        $req = new PurgePathCacheRequest();
        $req->setPaths($paths);
        $req->setFlushType($flushType);

        $resp = $client->PurgePathCache($req);
		// 将返回的JSON字符串解析成PHP数组
		$responseArray = json_decode($resp->toJsonString(), true);
		// 获取任务ID和请求ID
		$taskId = $responseArray['TaskId'] ?? '未知任务ID';
		$requestId = $responseArray['RequestId'] ?? '未知请求ID';
        //echo "<pre>刷新腾讯云CDN资源提交成功:\n" . $resp->toJsonString() . "</pre>";
		// 格式化输出
		echo "<pre>刷新腾讯云CDN资源提交成功:\n任务ID: {$taskId}\n请求ID: {$requestId}\n预计1分钟后CDN资源将会刷新预热完成。</pre>";
    } catch (TencentCloudSDKException $e) {
        //echo "Error occurred: " . $e;
		// 将返回的JSON字符串解析成PHP数组
		$errorArray = [
        'ErrorOccurred' => get_class($e),
        'Code' => $e->getErrorCode(),
        'Message' => $e->getMessage(),
        'RequestId' => $e->getRequestId(),
		];
		// 格式化输出错误信息
		echo "<pre>对不起,出错了!\n错误类型: {$errorArray['ErrorOccurred']}\n错误代码: {$errorArray['Code']}\n错误信息: {$errorArray['Message']}\n请求ID: {$errorArray['RequestId']}</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)"> 刷新URL</label>
        <label><input type="radio" name="flushType" value="directory" checked 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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值