Skip to content

Commit

Permalink
Add Cloudflare API Token support for auth, domain optional if zone_id…
Browse files Browse the repository at this point in the history
… provided (#3406)

Signed-off-by: AbhishekKr <abhikumar163@gmail.com>

Signed-off-by: AbhishekKr <abhikumar163@gmail.com>
  • Loading branch information
abhishekkr authored Dec 15, 2022
1 parent 60e5e35 commit b0207b2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
15 changes: 10 additions & 5 deletions contrib/cloudflare.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
- `service_key` – Cloudflare Service Key. If this is not provided, use api_key and email.
- `api_key` – Cloudflare API key generated on the "My Account" page.
- `email` – Cloudflare Email address associated with your account.
- `domain` – The domain you want to clear
- `api_token` – Cloudflare API Token generated on the "My Account" page.
- `domain` – The domain you want to clear (optional if zone_id is provided).
- `zone_id` – Cloudflare Zone ID (optional).
### Usage
Expand All @@ -33,16 +34,16 @@
'X-Auth-Key' => $config['api_key'],
'X-Auth-Email' => $config['email']
];
} else if (!empty($config['api_token'])) {
$headers = [
'Authorization' => 'Bearer '.$config['api_token']
];
} else {
throw new \RuntimeException("Set a service key or email / api key");
}

$headers['Content-Type'] = 'application/json';

if (empty($config['domain'])) {
throw new \RuntimeException("Set a domain");
}

$makeRequest = function ($url, $opts = []) use ($headers) {
$ch = curl_init("https://api.cloudflare.com/client/v4/$url");

Expand Down Expand Up @@ -71,6 +72,10 @@

$zoneId = $config['zone_id'];
if (empty($zoneId)) {
if (empty($config['domain'])) {
throw new \RuntimeException("Set a domain");
}

// get the mysterious zone id from Cloud Flare
$zones = json_decode($makeRequest(
"zones?name={$config['domain']}"
Expand Down
5 changes: 3 additions & 2 deletions docs/contrib/cloudflare.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ require 'contrib/cloudflare.php';
- `service_key` – Cloudflare Service Key. If this is not provided, use api_key and email.
- `api_key` – Cloudflare API key generated on the "My Account" page.
- `email` – Cloudflare Email address associated with your account.
- `domain` – The domain you want to clear
- `api_token` – Cloudflare API Token generated on the "My Account" page.
- `domain` – The domain you want to clear (optional if zone_id is provided).
- `zone_id` – Cloudflare Zone ID (optional).

### Usage
Expand All @@ -33,7 +34,7 @@ if not the, last tasks before cleanup
## Tasks

### deploy:cloudflare
[Source](https://github.com/deployphp/deployer/blob/master/contrib/cloudflare.php#L22)
[Source](https://github.com/deployphp/deployer/blob/master/contrib/cloudflare.php#L23)

Clears Cloudflare Cache.

Expand Down

0 comments on commit b0207b2

Please sign in to comment.