如何将数据发送到 CloudFlare API?

How to send data to CloudFlare API?

我正在尝试使用 PHP 从我的 CloudFlare 缓存中删除文件。使用 Guzzle 我已经这样做了:

$client = new \GuzzleHttp\Client;
$response = $client->delete('https://api.cloudflare.com/client/v4/zones/myzoneid/purge_cache', [
    'query' => [
        'files' => 'https://example.com/styles.css,
    ],
    'headers' => [
        'X-Auth-Email' => 'myemail',
        'X-Auth-Key' => 'myapikey',
    ],
]);

但是当我运行这个时我得到一个错误:

Client error: DELETE https://api.cloudflare.com/client/v4/zones/myzoneid/purge_cache?files=https%3A%2F%2Fexample.com/etc resulted in a 400 Bad Request response: {"success":false,"errors":[{"code":1012,"message":"Request must contain one of \"purge_everything\", \"files\", \"tags\" (truncated...)

我也无法使用 Postman 让它工作。我输入了所需的 headers 并尝试使用 URL 设置 filesfiles[] 的键,但它不起作用。我也试过 data 与原始 JSON 作为价值 {"files":["url"]} (连同 JSON content-type header)但得到相同错误。它认为我没有发送 files 密钥。

所以正确的语法应该是....

$client = new \GuzzleHttp\Client;
$response = $client->post('https://api.cloudflare.com/client/v4/zones/myzoneid/purge_cache', [
    'json' => [
        'files' => ['https://example.com/styles.css'],
    ],
    'headers' => [
        'X-Auth-Email' => 'myemail',
        'X-Auth-Key' => 'myapikey',
    ],
]);