1. Go to this page and download the library: Download hampel/cloudflare-api library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hampel / cloudflare-api example snippets
use GuzzleHttp\Client as Guzzle;
use Hampel\Cloudflare\Api\Client;
use Hampel\Cloudflare\Api\Entity\DnsRecord;
$cloudflare = Client::withToken('MY-API-TOKEN', new Guzzle());
$cloudflare->verify(); // does this token work?
$zone = $cloudflare->zones()->getByName('example.com');
foreach ($cloudflare->zones()->records($zone->id)->each() as $record) {
echo $record->describe(), "\n";
}
$cloudflare->zones()->records($zone->id)
->create(DnsRecord::a($zone->fqdn('www'), '203.0.113.10')->proxy());
use GuzzleHttp\Psr7\HttpFactory;
use Hampel\Cloudflare\Api\Authentication\ApiToken;
use Hampel\Cloudflare\Api\Config;
$factory = new HttpFactory(); // PSR-17, fills both the request and stream roles
$cloudflare = new Client(
new Config(pageSize: 50), // 5-50 if you set it at all - see Pagination
new ApiToken('MY-API-TOKEN'),
new Guzzle(),
$factory,
$factory,
$logger, // PSR-3, optional
);
$token = $cloudflare->verify();
$token->isActive(); // real, and usable right now
$token->expiresWithinDays(30);
$token->summary(); // one line, carrying no part of the secret
$cloudflare->verify();
$cloudflare->zones()->list(1, 5); // raises NotAuthenticatedException if refused from here
$cloudflare->zones()->list(); // one page
$cloudflare->zones()->each(); // a generator over every page
$cloudflare->zones()->all();
$cloudflare->zones()->get($zoneId);
$cloudflare->zones()->find($zoneId); // null when absent
$cloudflare->zones()->findByName('example.com'); // null when absent
$cloudflare->zones()->getByName('example.com'); // raises when absent
$zone->isActive(); // Cloudflare is answering for this domain
$zone->isPending(); // nameservers not yet pointed at Cloudflare
$zone->isPaused(); // zone-wide: every record served DNS-only
$zone->nameServers;
$zone->planLegacyId; // 'free', 'pro' - stable, where planName is display text
$zone->fqdn('www'); // www.example.com
$zone->fqdn(''); // example.com - the apex
$records = $cloudflare->zones()->records($zoneId);
$records->list();
$records->each();
$records->all();
$records->ofType(RecordType::MX);
$records->named('www.example.com');
$records->get($recordId);
$records->find($recordId); // null when absent
$records->create($record);
$records->patch($recordId, ['ttl' => 300]);
$records->replace($recordId, $record);
$records->delete($recordId);
$records->export(); // the zone as a BIND file
DnsRecord::a('www.example.com', '203.0.113.10');
DnsRecord::aaaa('www.example.com', '2001:db8::1');
DnsRecord::cname('blog.example.com', 'example.ghost.io');
DnsRecord::mx('example.com', 'mail.example.com', priority: 10);
DnsRecord::txt('_dmarc.example.com', 'v=DMARC1; p=quarantine');
DnsRecord::ns('sub.example.com', 'ns1.elsewhere.com');
DnsRecord::ptr('10.113.0.203.in-addr.arpa', 'www.example.com');
DnsRecord::caa('example.com', CaaTag::Issue, 'letsencrypt.org');
DnsRecord::srv('_sip._tcp.example.com', 'sip.example.com', port: 5060, priority: 10, weight: 5);
DnsRecord::of(RecordType::OPENPGPKEY, $name, $content); // any other content type
DnsRecord::components(RecordType::TLSA, $name, [...]); // any other data type
$record->withTtl(3600)->withComment('why this exists')->withTags(['production']); // tags need a paid plan
$record->proxy(); // A, AAAA and CNAME only
$record->unproxy(); // sends proxied: false rather than leaving it to the default
$registrations = $cloudflare->registrations();
$registrations->each($zone->accountId); // a generator, walked by cursor
$registrations->all($zone->accountId);
$registrations->get($zone->accountId, 'example.com'); // raises NotFoundException if not held
$registrations->find($zone->accountId, 'example.com'); // null if not held
$registration->domainName;
$registration->status; // 'active'
$registration->expiresAt; // DateTimeImmutable, UTC
$registration->autoRenew;
$registration->locked;
$registration->privacyMode; // 'redaction'
$registration->isActive();
$registration->expiresWithinDays(90);
$registration->lapsesWithoutAction(); // auto-renew explicitly off
$page = $records->list(page: 2, pageSize: 100);
$page->items;
$page->count(); // on this page
$page->total(); // across every page
$page->hasMore();
$page->currentPage();
$page->lastPage();
$meta = $response->meta;
$meta->rateLimit;
$meta->rateLimitRemaining;
$meta->rateLimitResetsIn;
$meta->isNearingRateLimit(); // false when the headers were absent
$meta->ray; // CF-Ray, for a support ticket