1

How to make delete api call to cloudflare without CURL in PHP?

My hosting provider doesn't provide me Curl service


I am especially interested and wanna make php api call to CloudFlare to Purge All files from cache.

On api page I found

enter image description here

Then did research and research still research and found a way (maybe) by doing

<?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;

But got error on this

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

I tried also this way:

<?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;

But good also error, this time:

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

And when I get on browser to my https://api.cloudflare.com/client/v4/zones/MYAPIID/purge_cache i see

enter image description here

Reachable but doesn't work, something is bad here or it is even not possible at all.

So is it event possible to make this thing without CURL? And If yes how to do that?

Krystian Polska
  • 1,286
  • 3
  • 15
  • 27
  • What's wrong with actually using cURL? – BenM Sep 23 '17 at 17:54
  • @BenM my hosting doesn't provide me Curl – Krystian Polska Sep 23 '17 at 17:54
  • @MarcinOrlowski dont wanna change my hosting provider. Wanna solve this in php without curl if possible – Krystian Polska Sep 24 '17 at 10:20
  • 1
    My point is changing would most likely be cheaper than wasting time reinventing the wheel – Marcin Orlowski Sep 24 '17 at 11:30
  • 1
    yeah seriously, contact your webhost customer support, and get them to add curl support, or if they really can't do that, they're a shitty webhost and you should find a new one. – hanshenrik Sep 24 '17 at 12:24
  • Don't hide the errors while debugging. Also make file_get_contents to return the response body as string (it might contain more information on problems with the request), set the `ignore_errors` HTTP wrapper option to `true` (default is `false`). You will then always get a string back from `file_get_contents`. – hakre Sep 24 '17 at 14:09
  • @hakre I am not hidding errors – Krystian Polska Sep 24 '17 at 16:17
  • @hakre after adding `'ignore_errors' => true,` i get nothing, blank – Krystian Polska Sep 24 '17 at 16:26
  • @hakre or not, var_dumped this, got `E:\XAMPP\htdocs\test.php:17:string '{"success":false,"errors":[{"code":1012,"message":"Request must contain one of \"purge_everything\", \"files\", \"tags\", or \"hosts\""}],"messages":[],"result":null}' (length=166)` – Krystian Polska Sep 24 '17 at 16:31
  • @KrystianPolska: Please try to replace the ` data` key in the HTTP context options array with `content`, that will be send. See [the PHP Manual entry for all **HTTP context options**](http://php.net/manual/en/context.http.php) for a description of each, I could not spot a *data* entry, but *content* is used for the request body which sends the data and this error somewhat explains the error message you commented. – hakre Sep 27 '17 at 16:25

2 Answers2

2

PHP HTTP wrapper is not Curl, so you need to translate the --data from the Curl command-line switch to the appropriate HTTP context option in PHP.

The entry in PHP is "content". Using it instead of "data" should do it.

Example:

$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

See as well:

hakre
  • 193,403
  • 52
  • 435
  • 836
-2

looks like some firewall is preventing file_get_contents from connecting to the outside web. your last code, with file_get_contents, should have worked. contact your webhost customer support, and ask them to unblock your php script from accessing the web. oh, and get them to add curl support, or if they really can't do that, they're a shitty webhost and you should find a new one.

hanshenrik
  • 19,904
  • 4
  • 43
  • 89