【问题标题】:Purging Cloudflare cache through their API in php通过他们在 php 中的 API 清除 Cloudflare 缓存
【发布时间】:2019-09-28 05:10:54
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: php json wordpress caching cloudflare


    【解决方案1】:

    我写了一个小而肯定可以改进的 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!";
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-03
      • 2017-07-16
      • 1970-01-01
      • 2018-04-07
      • 1970-01-01
      • 1970-01-01
      • 2015-07-12
      • 2012-03-30
      相关资源
      最近更新 更多