【问题标题】:Using cloudflare api to delete a file from cache not working使用 cloudflare api 从缓存中删除文件不起作用
【发布时间】:2017-02-10 13:39:45
【问题描述】:

这是我尝试通过 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: user@example.com" \
     -H "X-Auth-Key: c2547eb745079dac9320b638f5e225cf483cc5cfdda41" \
     -H "Content-Type: application/json" \
     --data '{"files":["http://www.example.com/css/styles.css"],"tags":["some-tag","another-tag"]}'

这里做错了什么?

【问题讨论】:

    标签: php curl cloudflare


    【解决方案1】:

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

    试试

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

    【讨论】:

    • 您可以删除http_build_query 并重新测试吗?
    • 删除 http_build_querycurl_setopt($ch, CURLOPT_POST, true); 修复了它
    【解决方案2】:

    这有点令人困惑。发布数据通常以键值对的形式发送。
    此外,当 post 数据是数组 curl 时,会将内容类型更改为 multipart/form-data

    当post数据作为查询字符串发送时,格式为key1=value1&amp;key2=value2

    看来 json 是没有键的值。

    我会尝试将其作为数组和字符串同时查看请求标头。
    在请求标头中查看内容类型和数据。

    要在 curl 返回中获取请求标头,请添加此选项:
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);

    您可能需要添加:
    curl_setopt($ch, CURLOPT_POST, true);

    $data[] = json_encode(array('files'=>"https://example.com/file/$filename"));
    curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
    
    $data = json_encode(array('files'=>"https://example.com/file/$filename"));
    curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
    

    PS:您不需要使用 . $filename 使用双引号时的连接

    【讨论】:

      猜你喜欢
      • 2016-05-20
      • 1970-01-01
      • 2021-11-02
      • 2014-08-29
      • 2017-07-16
      • 1970-01-01
      • 2018-07-03
      • 2012-04-22
      • 2011-06-11
      相关资源
      最近更新 更多