如何使 php api 调用 CloudFlare 以通过 URL 从缓存中清除单个文件

gan*_*ali 3 api purge cloudflare

这段代码可以很好地清除缓存中的所有文件。

例子:

$authKey = "MyKEY";
$authEmail = "myEMAIL";

$zoneId = "MYZONEID";
$endpoint = "purge_cache";

$data = [
    "purge_everything" => true
];

$url = "https://api.cloudflare.com/client/v4/zones/{$zoneId}/{$endpoint}";
$opts = ['http' => [
    'method' => 'DELETE',
    'header' => [
        "Content-Type: application/json",
        "X-Auth-Key: {$authKey}",
        "X-Auth-Email: {$authEmail}",
    ],
    'content' => json_encode($data),
]];
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);
Run Code Online (Sandbox Code Playgroud)

但是我需要通过 URL 清除单个文件我怎么能通过 PHP 做到这一点?

在 api 页面上我找到了这个链接:https : //api.cloudflare.com/#zone-purge-individual-files-by-url

但我现在不知道如何通过 php 制作它?

Ale*_*x T 5

您需要传递一个包含文件的数组 - 如API 文档中所述

在您的示例中,这将类似于

$data = '{"files":[
  "http://www.example.com/css/styles1.css",
  "http://www.example.com/css/styles2.css",
  "http://www.example.com/css/styles3.css"
]}';
Run Code Online (Sandbox Code Playgroud)

  • 谢谢@AlexT,当我想看看参数叫什么时,CF文档没有它,只有标签、前缀和主机。 (2认同)