Viewing file: CloudflareAPIHelper.php (1.92 KB) -rw-r--r-- Select action/file-type: (+) | (+) | (+) | Code (+) | Session (+) | (+) | SDB (+) | (+) | (+) | (+) | (+) | (+) |
<?php
namespace App\Helpers;
use GuzzleHttp\Client;
class CloudflareAPIHelper
{
private $client;
private $accountId;
private $apiKey;
public function __construct()
{
$this->client = new Client([
'base_uri' => 'https://api.cloudflare.com/client/v4/accounts/',
'headers' => [
'Authorization' => 'Bearer ' . env('CLOUDFLARE_API_KEY'),
'Content-Type' => 'application/json',
],
]);
$this->accountId = env('CLOUDFLARE_ACCOUNT_ID');
$this->apiKey = env('CLOUDFLARE_API_KEY');
}
public function put($namespaceId, $key, $value)
{
$response = $this->client->request(
'PUT',
$this->buildEndpoint($namespaceId, $key),
[
// 'json' => 'value' //for string
'json' => $value,
]
);
return $response->getStatusCode() === 200;
}
public function get($namespaceId, $key)
{
try {
$response = $this->client->request(
'GET',
$this->buildEndpoint($namespaceId, $key)
);
$content = json_decode($response->getBody()->getContents(), true);
if ($response->getStatusCode() === 200 ) {
return $content;
}
return null;
}catch (\Exception $exception) {
return null;
}
}
public function delete($namespaceId, $key)
{
$response = $this->client->request(
'DELETE',
$this->buildEndpoint($namespaceId, $key)
);
return $response->getStatusCode() === 204;
}
private function buildEndpoint($namespaceId, $key)
{
return $this->accountId . '/storage/kv/namespaces/' . $namespaceId . '/values/' . $key;
}
}
|