Api 使用 PHP 但不使用 Curl 调用 CloudFlare

Api call to CloudFlare with PHP but without Curl

如何在 PHP 中不使用 CURL 删除对 cloudflare 的 api 调用?

我的托管服务提供商不为我提供 Curl 服务


我特别感兴趣,想 php api 调用 CloudFlare 以清除缓存中的所有文件。

我在 api 页面上找到了

然后研究和研究仍在研究并通过做

找到了一个方法(也许)
<?php
$data = array (
    "purge_everything"  =>  true
);
$url = "https://api.cloudflare.com/client/v4/zones/MYZONEID/purge_cache";
$opts = array('http' =>
                  array(
                      'method'  => 'DELETE',
                      'header'  => "Content-Type: application/json\r\n" . "X-Auth-Key: MYKEY\r\n" . "X-Auth-Email: MYEMAIL\r\n",
                      'data' => json_encode($data)
                  )
);
$context  = stream_context_create($opts);
$fp = @fopen($url, 'rb', false, $context);
if (!$fp) {
    throw new Exception("Problem with $url, $php_errormsg");
}
$response = @stream_get_contents($fp);
if ($response === false) {
    throw new Exception("Problem reading data from $url, $php_errormsg");
}
return $response;

但是在这方面出错

Fatal error: Uncaught Exception: Problem with https://api.cloudflare.com/client/v4/zones/MYZONEID/purge_cache, in /srv/disk11/2444530/www/xxxx.pl/test.php:16 Stack trace: #0 {main} thrown in /srv/disk11/2444530/www/xxxx.pl/test.php on line 16

我也这样试过:

<?php
$data = array (
    "purge_everything"  =>  true
);
$method = "getCallDetails";
$url = "https://api.cloudflare.com/client/v4/zones/MYZONEID/purge_cache";
$opts = array('http' =>
                  array(
                      'method'  => 'DELETE',
                      'header'  => "Content-Type: application/json\r\n" . "X-Auth-Key: MyKEY\r\n" . "X-Auth-Email: myEMAIL\r\n",
                      'data' => json_encode($data)
                  )
);
$context  = stream_context_create($opts);
$result = file_get_contents($url, false, $context);

return $result;

不过还好还报错,这次:

Warning: file_get_contents(https://api.cloudflare.com/client/v4/zones/MYAPIID/purge_cache): failed to open stream: Network is unreachable in /srv/disk11/2444530/www/xxxx.pl/test.php on line 15

当我在浏览器上访问 https://api.cloudflare.com/client/v4/zones/MYAPIID/purge_cache 时,我看到

可访问但不工作,这里有问题或者根本不可能。

那么有没有可能在没有 CURL 的情况下制作这个东西?如果是,该怎么做?

似乎某些防火墙正在阻止 file_get_contents 连接到外部网络。您的最后一个代码 file_get_contents 应该有效。联系您的虚拟主机客户支持,并要求他们取消阻止您的 php 脚本访问网络。哦,让他们添加 curl 支持,或者如果他们真的做不到,那他们就是一个糟糕的虚拟主机,你应该找一个新的。

PHP HTTP 包装器不是 Curl,因此您需要将 --data 从 Curl 命令行开关转换为 PHP.[=17 中适当的 HTTP 上下文选项=]

PHP 中的条目是“content”。使用它而不是“data”应该可以做到。

示例:

$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);

# [...] parse response

另见: