PHP code example of gpenverne / cloudflare-bundle

1. Go to this page and download the library: Download gpenverne/cloudflare-bundle 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/ */

    

gpenverne / cloudflare-bundle example snippets


$cloudflareService = $this->container->get('cloudflare.service');
// Or ...
$cloudflareService = $this->container->get(Gpenverne\CloudflareBundle\Services\CloudflareService::class)
// Or inject in your constructors

// Retrieve a Cloudflare SDK endpoint
$userEndpoint = $cloudflareService->get('User');

// Or use built-in shortcut
$userEndpoint = $cloudflareService->user;

// Listing all zones
$zones = $cloudflareService->zones->listZones();

// Adapted example from extracted from https://support.cloudflare.com/hc/en-us/articles/115001661191
$zones = $cloudflareService->zones;
foreach ($zones->listZones()->result as $zone) {
    echo "Cache purge for " . $zone->name . ": ";
    echo $zones->cachePurgeEverything($zone->id) == true ? "successful" : "failed";
    echo PHP_EOL;

}

// Add a domain to a zone
try {
    return $cloudflareService->dns->addRecord(
        $zone->id,
        'CNAME',
        'my-subdomain',
        'my-domain.com'
    );
} catch (\Exception $e) {
    return false;
}