Cloudflare PHP API Binding

A translation of the article was prepared ahead of the start of the Backend PHP Developer course .




For those developers who use PHP 7.0 or higher, Cloudflare provides PHP API binders. It supports the latest fourth version of the Cloudflare API. You can use this SDK for a number of purposes, including such as:

  • Manage and automate changes to your DNS entries in Cloudflare.
  • Programmatically add zones to your account.
  • Versioning and managing changes in Page Rules
  • Automatically block IP addresses and User Agents using Zone Lockdown and UserAgent Rules.
  • Getting Cloudflare IP ranges to automate whitelisting with a firewall

Supported Features


This article provides some common examples of using the Cloudflare PHP Binding API; 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


Cloudflare PHP API binding is available on Packagist in cloudflare / sdk , which can be installed using Composer by calling composer require cloudflare / sdk :



Also, if you want to read the source code directly or contribute to the project, you can find the source code on GitHub in cloudflare / cloudflare-php repository .

Beginning of work


Here is a small example of working with the API

<?php

require_once('vendor/autoload.php');

$key = new \Cloudflare\API\Auth\APIKey('mjsa@junade.com', '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;


By running this simple script on the command line, we get the following output:



Listing Zones


Here's how to list all zones in an account using the following code:

<?php

require_once('vendor/autoload.php');

$key = new \Cloudflare\API\Auth\APIKey('mjsa@junade.com', '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 script through the command line will give the following output:



Clear cache on all websites


Here is another example that uses the Purge Cache endpoint to completely clear the cache on each website of our account (note that you can clear individual files that use the cache by using the cachePurge method instead of cachePurgeEverything):

<?php

require_once('vendor/autoload.php');

$key = new \Cloudflare\API\Auth\APIKey('mjsa@junade.com', '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;

}

You will see the following output as a result of running this script on the command line:



Creating Page Rules


The SDK can also be used to programmatically add Page Rules to the Cloudflare Zone. Here is a simple example of adding a Cache Bypass rule:

<?php

require_once('vendor/autoload.php');

$key = new \Cloudflare\API\Auth\APIKey('mjsa@junade.com', '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 can easily get the ID of the desired zone using the getZoneID method of the Zones endpoint class - this helper method returns the ID by the name of the zone.

Note that the SDK uses dependency injection to specify the purpose and configuration of page rules. This is why we need to pass instances of the PageRulesTargets and PageRuleActions classes to the createPageRule method.

DNS


The SDK can also be used to programmatically add records. Here is an example of adding a DNS record:

<?php

require_once('vendor/autoload.php');

$key = new \Cloudflare\API\Auth\APIKey('mjsa@junade.com', '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;
}

In addition, we can also delete, list and view the details of DNS records using this SDK. For example, let's create a simple script to list the type and name of each DNS record in our zone:

<?php

require_once('vendor/autoload.php');

$key = new \Cloudflare\API\Auth\APIKey('mjsa@junade.com', '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;
}

This will be the output when I run this script for one of my zones:





Learn more about the course



All Articles