如何将数据发送到CloudFlare API?

时间:2017-10-12 00:38:37

标签: php guzzle cloudflare

我尝试使用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',
    ],
]);

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

  

客户端错误:DELETE https://api.cloudflare.com/client/v4/zones/myzoneid/purge_cache?files=https%3A%2F%2Fexample.com/etc导致400 Bad Request响应:{"成功":false,"错误":[{"代码&# 34;:1012,"消息":"请求必须包含\" purge_everything \",\" files \",\&# 34;标记\" (截...)

我也无法使用Postman工作。我输入了所需的标题,并尝试使用网址设置filesfiles[]的密钥,但它不起作用。我还尝试data使用原始JSON作为{"files":["url"]}之类的值(以及JSON内容类型标头)但得到相同的错误。它认为我没有发送files密钥。

1 个答案:

答案 0 :(得分:0)

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

$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',
    ],
]);