清除缓存子目录Cloudflare

时间:2018-12-15 12:13:26

标签: php cloudflare

我正在使用清除特定目录子目录缓存的函数。我正在将cloudflare与服务器配合使用 如果有人有主意 这是我的文章

<?php
class Custom_Tools_Cflare
{
    public function clear($dir,$host,$uri) {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, "https://api.cloudflare.com/client/v4/zones/xxxxxxxxxx/purge_cache");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"files\":[\"http://xxxx.com/".$dir."/".$uri."\",{\"url\":\"http://xxxx.com/".$dir."/".$uri."\",\"headers\":{\"X-Forwarded-Host\":\"".$host."\",\"X-Host\":\"xxxxxxx.com\"}}]}");
        curl_setopt($ch, CURLOPT_POST, 1);

        $headers = array();
        $headers[] = "X-Auth-Email: xxxx@xxxx.com";
        $headers[] = "X-Auth-Key: xxxxxxxxxxx";
        $headers[] = "Content-Type: application/json";
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

        $result = curl_exec($ch);
        if (curl_errno($ch)) {
            echo 'Error:' . curl_error($ch);
        }
        curl_close ($ch);
    }
    public function myfunction(){

        $di = new RecursiveDirectoryIterator('http://xxxxx.com/');
        foreach (new RecursiveIteratorIterator($di) as $filename => $file) {
        $file->clear($di,$host,$uri);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您可以使用我编写的名为pu​​rge-cloudflare-cache的模块清除子目录缓存:

答案在这里:

clearing cloudflare cache programmatically