php - 使用 cloudflare api 从缓存中删除文件不起作用

标签 php curl cloudflare

这是我尝试通过 api 删除文件的代码

$ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://api.cloudflare.com/client/v4/zones/<MY-ZONE-ID>/purge_cache");
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $headers = [
            'X-Auth-Email:  MYEMAIL',
            'X-Auth-Key: MY-AUTH-KEY',
            'Content-Type: application/json'
        ];

        $data = json_encode(array("files" => "https://example.com/file/".$filename));
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $result = curl_exec($ch);

我得到的回复如下

{
   "success":false,
   "errors":[
      {
         "code":1012,
         "message":"Request must contain one of \"purge_everything\" or \"files\", or \"tags"
      }
   ],
   "messages":[

   ],
   "result":null
}

文档说标签是可选的,因此它应该可以工作

curl -X DELETE "https://api.cloudflare.com/client/v4/zones/023e105f4ecef8ad9ca31a8372d0c353/purge_cache" \
     -H "X-Auth-Email: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="582d2b3d2a183d20393528343d763b3735" rel="noreferrer noopener nofollow">[email protected]</a>" \
     -H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \
     -H "Content-Type: application/json" \
     --data '{"files":["http://www.example.com/css/styles.css"],"tags":["some-tag","another-tag"]}'

这里做错了什么?

最佳答案

我可能是因为你的 files 属性不是数组。

尝试

$data = json_encode(array("files" => array("https://example.com/file/".$filename)));

关于php - 使用 cloudflare api 从缓存中删除文件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42160784/

相关文章:

php - 按元数据的值对用户进行排序

php - 如何在wordpress中使用php调用多个sp

php - javascript/jquery : get info for a remote image

php - 使用 curl 从 SSL 下载图像?

javascript - CloudFlare Workers - 检查 Cookie、添加 header 、设置 Cookie

php - 优化 PHP If/Else 语句

php - 如何 header stream_context_create?

linux - 无法连接到多线程 CURL 中的主机(错误 7)mipsel-linux mipsel 多核架构师

php - Cloudflare header 返回 IPV6 地址

ssl - 使用具有自定义域的 github 页面时出现混合内容错误