Cloudflare PHP API Binding

Translation of the article prepared in advance of the start of the course “Backend PHP Developer”.


For those developers using 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 available on packagist in cloudflare / sdkthat can be installed using composer with a call 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 the repository cloudflare / cloudflare-php.

Beginning of work

Here is a small example of working with the API

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:

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):

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 SDKs can also be used to programmatically add Page Rules to the Cloudflare Zone. Here is a simple example of adding a Cache Bypass rule:

getZoneID("junade.com");

$pageRulesTarget = new CloudflareAPIConfigurationsPageRulesTargets('https://junade.com/noCache/*');

$pageRulesConfig = new CloudflareAPIConfigurationsPageRulesActions();
$pageRulesConfig->setCacheLevel('bypass');

$pageRules = new CloudflareAPIEndpointsPageRules($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 the 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:

getZoneID("junade.com");

$dns = new CloudflareAPIEndpointsDNS($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 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:

getZoneID("icyapril.com");

$dns = new CloudflareAPIEndpointsDNS($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


Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *