繁体   English   中英

通过 php 中的 API 清除 Cloudflare 缓存

[英]Purging Cloudflare cache through their API in php

我在朋友的网站上遇到了这个问题。 这是一个带有多个插件的 Wordpress 安装。 其中一个插件用于更新多个图像(从远程位置收集它们并将它们存储在本地以节省带宽)。 但是在运行插件时,该网站似乎拒绝显示更新的图像并不断给我旧版本,这些旧版本肯定不再存在于服务器上。

浏览器缓存被迅速排除为原因。 Wordpress 可能有点棘手,所以我检查了所有其他插件、插件以及是否有任何形式的对象缓存处于活动状态。 在排除了这一点之后,我发现托管服务提供商一定是问题所在。 我不知道并且不得不发现他们使用 Cloudflare 作为 DNS 提供商来为其网站提供有效的 SSL 证书。 然而,默认情况下 Cloudflare 还带有缓存,这可能非常激进。

由于他们喜欢缓存并希望保持打开状态,我告诉我的朋友在 Cloudflare 手动清除缓存。 Ta-Da - 更新后的图像显示正常。

因此,为了避免每次调用插件时都登录 Cloudflare 的过程,我正在寻找一种使用他们的 API 以方便的方式解决此问题的方法。 我需要一些 php 代码(以集成到 Wordpress 插件中)...

我写了一个小而肯定可以改进的 php 脚本,它正是用于这个目的。 它使用给定的凭据(用户电子邮件和 API 密钥)连接到 Cloudflare 的 API。 要检索 API 密钥:

  1. 登录到 Cloudflare 帐户。

  2. 转到我的个人资料

  3. 向下滚动到 API Keys并找到Global API Key

  4. 单击 API 密钥以查看您的 API 标识符。

在第一步中,脚本查询所谓的 Zone-ID,它是您要控制的域的唯一标识符。 由于迄今为止 Cloudflare 没有提供在后端查看此 ID 的选项,因此只能通过 API 请求获取。

在第二步中,我们再次连接到 Cloudflare 的 API,这次指示清除该区域的整个缓存。

这是我的解决方案(我把它放在我的插件更新程序脚本的底部,在其他一切完成后运行):

<?php

    //Credentials for Cloudflare
    $cust_email = ''; //user@domain.tld
    $cust_xauth = ''; //retrieved from the backend after loggin in
    $cust_domain = ''; //domain.tld, the domain you want to control

    if($cust_email == "" || $cust_xauth == "" || $cust_domain == "") return;

    //Get the Zone-ID from Cloudflare since they don't provide that in the Backend
    $ch_query = curl_init();
    curl_setopt($ch_query, CURLOPT_URL, "https://api.cloudflare.com/client/v4/zones?name=".$cust_domain."&status=active&page=1&per_page=5&order=status&direction=desc&match=all");
    curl_setopt($ch_query, CURLOPT_RETURNTRANSFER, 1);
    $qheaders = array(
        'X-Auth-Email: '.$cust_email.'',
        'X-Auth-Key: '.$cust_xauth.'',
        'Content-Type: application/json'
    );
    curl_setopt($ch_query, CURLOPT_HTTPHEADER, $qheaders);
    $qresult = json_decode(curl_exec($ch_query),true);
    curl_close($ch_query);

    $cust_zone = $qresult['result'][0]['id']; 

    //Purge the entire cache via API
    $ch_purge = curl_init();
    curl_setopt($ch_purge, CURLOPT_URL, "https://api.cloudflare.com/client/v4/zones/".$cust_zone."/purge_cache");
    curl_setopt($ch_purge, CURLOPT_CUSTOMREQUEST, "DELETE");
    curl_setopt($ch_purge, CURLOPT_RETURNTRANSFER, 1);
    $headers = [
        'X-Auth-Email: '.$cust_email,
        'X-Auth-Key: '.$cust_xauth,
        'Content-Type: application/json'
    ];
    $data = json_encode(array("purge_everything" => true));
    curl_setopt($ch_purge, CURLOPT_POST, true);
    curl_setopt($ch_purge, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch_purge, CURLOPT_HTTPHEADER, $headers);

    $result = json_decode(curl_exec($ch_purge),true);
    curl_close($ch_purge);

    //Tell the user if it worked
    if($result['success']==1) echo "Cloudflare Cache successfully purged! Changes should be visible right away.<br>If not try clearing your Browser Cache by pressing \"Ctrl+F5\"";
    else echo "Error purging Cloudflare Cache. Please log into Cloudflare and purge manually!";

?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM