PHP code example of secretwebmaster / laravel-cloudflare

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

    

secretwebmaster / laravel-cloudflare example snippets


use Secretwebmaster\LaravelCloudflare\Client;

// Option 1: Create using .env values
$client = new Client();

// Option 2: Create manually with API Token
$client = new Client('your-api-token');

// Option 3: Create manually with Email + API Key
$client = new Client(null, '[email protected]', 'your-api-key');

// List all zones
$response = $client->zones()->list();

// List zones with pagination
$zones = $client->zones()->list([
    'page' => 1,
    'per_page' => 20
]);

// Filter zones by status
$zones = $client->zones()->list([
    'status' => 'active'
]);

// Get zone information by domain name
$zone = $client->zones()->getByDomain('example.com');

// Quickly get just the zone ID
$zoneId = $client->zones()->getZoneIdByDomain('example.com');

$response = $client->zones()->create([
    'name' => 'example.com',
]);

$response = $client->zones()->get($zoneId);

$client->zones()->edit($zoneId, [
    'paused' => false,
    'plan' => [
        'id' => 'plan-id'
    ]
]);

// Purge everything
$client->zones()->purgeCache($zoneId, ['purge_everything' => true]);

// Purge specific files
$client->zones()->purgeCache($zoneId, [
    'files' => [
        'https://example.com/style.css',
        'https://example.com/script.js'
    ]
]);

// Purge by tags
$client->zones()->purgeCache($zoneId, [
    'tags' => ['static', 'images']
]);

// Purge by hostname
$client->zones()->purgeCache($zoneId, [
    'hosts' => ['www.example.com', 'api.example.com']
]);

$response = $client->zones()->activationCheck($zoneId);

$response = $client->zones()->delete($zoneId);

// List all DNS records for a zone
$response = $client->dnsRecords()->list($zoneId);

// Filter by record type
$records = $client->dnsRecords()->list($zoneId, [
    'type' => 'A'
]);

// Filter by name
$records = $client->dnsRecords()->list($zoneId, [
    'name' => 'www.example.com'
]);

// Combine filters with pagination
$records = $client->dnsRecords()->list($zoneId, [
    'type' => 'A',
    'page' => 1,
    'per_page' => 50
]);

// Create an A record
$record = $client->dnsRecords()->create($zoneId, [
    'type' => 'A',
    'name' => 'www.example.com',
    'content' => '192.168.1.1',
    'ttl' => 1,  // 1 = Auto
    'proxied' => true
]);

// Create a CNAME record
$record = $client->dnsRecords()->create($zoneId, [
    'type' => 'CNAME',
    'name' => 'blog',
    'content' => 'example.com',
    'ttl' => 1,
    'proxied' => false
]);

// Create an MX record
$record = $client->dnsRecords()->create($zoneId, [
    'type' => 'MX',
    'name' => 'example.com',
    'content' => 'mail.example.com',
    'priority' => 10,
    'ttl' => 1
]);

// Create a TXT record
$record = $client->dnsRecords()->create($zoneId, [
    'type' => 'TXT',
    'name' => '_dmarc',
    'content' => 'v=DMARC1; p=reject; rua=mailto:[email protected]',
    'ttl' => 1
]);

$response = $client->dnsRecords()->get($zoneId, $recordId);

// Update entire record
$client->dnsRecords()->update($zoneId, $recordId, [
    'type' => 'A',
    'name' => 'www.example.com',
    'content' => '192.168.1.2',
    'ttl' => 1,
    'proxied' => true
]);

// Update only specific fields
$client->dnsRecords()->edit($zoneId, $recordId, [
    'content' => '192.168.1.3',
    'proxied' => false
]);

// Just change TTL
$client->dnsRecords()->edit($zoneId, $recordId, [
    'ttl' => 3600
]);

$response = $client->dnsRecords()->delete($zoneId, $recordId);

// Get SSL setting
$response = $client->zoneSettings()->getSsl($zoneId);

// Update SSL setting
$client->zoneSettings()->updateSsl($zoneId, ['value' => 'full']);

// Get Always Use HTTPS
$alwaysHttps = $client->zoneSettings()->getAlwaysUseHttps($zoneId);

// Enable Always Use HTTPS
$client->zoneSettings()->updateAlwaysUseHttps($zoneId, ['value' => 'on']);

// Get and update multiple settings
$settings = $client->zoneSettings()->list($zoneId);

// Update any setting using the generic update method
$client->zoneSettings()->update($zoneId, 'minify', [
    'value' => [
        'css' => 'on',
        'html' => 'on',
        'js' => 'on'
    ]
]);

// Zone-level: Block an IP
$client->firewallAccessRules()->create($zoneId, [
    'mode' => 'block',
    'configuration' => [
        'target' => 'ip',
        'value' => '192.168.1.100'
    ],
    'notes' => 'Blocked spam source'
]);

// Zone-level: Whitelist an IP range
$client->firewallAccessRules()->create($zoneId, [
    'mode' => 'whitelist',
    'configuration' => [
        'target' => 'ip_range',
        'value' => '10.0.0.0/8'
    ],
    'notes' => 'Office IP range'
]);

// Account-level: Challenge by country
$client->firewallAccessRules()->createAccount($accountId, [
    'mode' => 'challenge',
    'configuration' => [
        'target' => 'country',
        'value' => 'CN'
    ]
]);

// List zone access rules
$rules = $client->firewallAccessRules()->list($zoneId);

// Delete a rule
$client->firewallAccessRules()->delete($zoneId, $ruleId);

// Create a page rule
$client->pageRules()->create($zoneId, [
    'targets' => [
        [
            'target' => 'url',
            'constraint' => [
                'operator' => 'matches',
                'value' => 'example.com/admin/*'
            ]
        ]
    ],
    'actions' => [
        [
            'id' => 'ssl',
            'value' => 'strict'
        ],
        [
            'id' => 'always_use_https',
            'value' => true
        ]
    ],
    'priority' => 1,
    'status' => 'active'
]);

// List all page rules
$rules = $client->pageRules()->list($zoneId);

// Get available settings
$settings = $client->pageRules()->getSettingList();

// List accounts
$accounts = $client->accounts()->list();

// Get account details
$account = $client->accounts()->get($accountId);

// Get user details
$user = $client->users()->get();

// Update user
$client->users()->update([
    'first_name' => 'John',
    'last_name' => 'Doe'
]);

$response = $client->zones()->create(['name' => 'example.com']);

// Get error code explanations
$errorCodes = $client->zones()->errorCodes();
echo $errorCodes['1061']; // "An A, AAAA or CNAME record already exists..."
bash
php artisan vendor:publish --tag=cloudflare-config