Cloudflare PHP API Binding

Cloudflare offers a PHP API binding for those using PHP 7.0 or greater, this binding supports the latest version 4 of the Cloudflare API. You can use this SDK for a variety of purposes, including:

  • Manage and automate changes to your DNS records within Cloudflare
  • Programatically add zones to your account
  • Version and change control for Page Rules
  • Automatically blocking IPs and User Agents using Zone Lockdown and UserAgent Rules
  • Fetch Cloudflare's IP ranges for automating your firewall whitelisting

Supported Features

This article explains a few common examples of using Cloudflare's PHP API Binding; we currently support the following endpoints and authentication methods: 

V4 Endpoints

  • DNS
  • IPs
  • Page Rules
  • User Agent Rules
  • User Management (partial)
  • Zone Lockdown
  • Zones

Authentication

  • User Service Keys
  • API Keys

Installation

The Cloudflare PHP API Binding is available on Packagist as cloudflare/sdk and can be installed using Composer by running composer require cloudflare/sdk:

installing-cloudflare-php.png

Alternatively should you wish to look directly at the source code or make contributions, you can find the original source code on GitHub in the cloudflare/cloudflare-php repository. 

Getting Started

Here is a simple example of using the API

<?php

require_once('vendor/autoload.php'); $key = new \Cloudflare\API\Auth\APIKey('[email protected]', 'API KEY GOES HERE'); $adapter = new Cloudflare\API\Adapter\Guzzle($key); $user = new \Cloudflare\API\Endpoints\User($adapter); echo 'Your user ID is: ' . $user->getUserID() . PHP_EOL;

Running this simple script via terminal yields the following output:

getting-user-id.png

Listing Zones

Here is a simple script to list all the zones on an account, using the following code:

<?php

require_once('vendor/autoload.php');

$key = new \Cloudflare\API\Auth\APIKey('[email protected]', 'API KEY GOES HERE');
$adapter = new Cloudflare\API\Adapter\Guzzle($key);

$zones = new \Cloudflare\API\Endpoints\Zones($adapter);
foreach ($zones->listZones()->result as $zone) {
echo $zone->name.' ('.$zone->plan->name.')'.PHP_EOL;
}

Running this via the command line yields the following output:

listing-zones.png

Purge Cache on Every Website

Here's another example which utilises the cache purge endpoint to clear the entire cache on every website in our account (note that you can purge individual files using the cache using the cachePurge method instead of cachePurgeEverything):

<?php

require_once('vendor/autoload.php');

$key = new \Cloudflare\API\Auth\APIKey('[email protected]', 'API KEY GOES HERE');
$adapter = new Cloudflare\API\Adapter\Guzzle($key);

$zones = new \Cloudflare\API\Endpoints\Zones($adapter);
foreach ($zones->listZones()->result as $zone) {
echo "Cache purge for " . $zone->name . ": ";
echo $zones->cachePurgeEverything($zone->id) == true ? "successful" : "failed";
echo PHP_EOL;

}

Here is the output of running this script via the command line:

php-cache-purge-everything.png

Creating Page Rules

The SDK can also be used for programatically adding Page Rules to a Cloudflare Zone, here's a simple example of adding a Cache Bypass rule:

<?php

require_once('vendor/autoload.php');

$key = new \Cloudflare\API\Auth\APIKey('[email protected]', 'API KEY GOES HERE');
$adapter = new Cloudflare\API\Adapter\Guzzle($key);
$zones = new \Cloudflare\API\Endpoints\Zones($adapter);

$zoneID = $zones->getZoneID("junade.com");

$pageRulesTarget = new \Cloudflare\API\Configurations\PageRulesTargets('https://junade.com/noCache/*');

$pageRulesConfig = new \Cloudflare\API\Configurations\PageRulesActions();
$pageRulesConfig->setCacheLevel('bypass');

$pageRules = new \Cloudflare\API\Endpoints\PageRules($adapter);
$pageRules->createPageRule($zoneID, $pageRulesTarget, $pageRulesConfig, true, 6);

We are able to easily get the ID of a given zone using the getZoneID method in the Zones endpoint class, this helper method makes it easier to get the zone ID from the zone name.

Note that the SDK uses dependency injection for specifying the target and the configuration of the page rules. That's why we need to pass instances of the PageRulesTargets and the PageRuleActions classes into the createPageRule method.

DNS

The SDK can also be used for programatically adding records, here's an example of adding an example DNS record:

<?php

require_once('vendor/autoload.php');

$key = new \Cloudflare\API\Auth\APIKey('[email protected]', 'API KEY GOES HERE');
$adapter = new Cloudflare\API\Adapter\Guzzle($key);
$zones = new \Cloudflare\API\Endpoints\Zones($adapter);

$zoneID = $zones->getZoneID("junade.com");

$dns = new \Cloudflare\API\Endpoints\DNS($adapter);
if ($dns->addRecord($zoneID, "A", 'example', '8.8.8.8', 0, true) === true) {
echo "DNS record created.". PHP_EOL;
}

Further, we can also delete, list and view details of DNS records through this SDK. For example; let's create a simple script to list the type and name of every DNS record on our zone:

<?php

require_once('vendor/autoload.php');

$key = new \Cloudflare\API\Auth\APIKey('[email protected]', 'API KEY GOES HERE');
$adapter = new Cloudflare\API\Adapter\Guzzle($key);
$zones = new \Cloudflare\API\Endpoints\Zones($adapter);

$zoneID = $zones->getZoneID("icyapril.com");

$dns = new \Cloudflare\API\Endpoints\DNS($adapter);
foreach ($dns->listRecords($zoneID)->result as $record) {
echo $record->type." ".$record->name.PHP_EOL;
}

Here's the example when I run this script against one of my zones:

php-list-dns-type-name.png

Still not finding what you need?

The Cloudflare team is here to help. 95% of questions can be answered using the search tool, but if you can’t find what you need, submit a support request.

Powered by Zendesk