This package is a simple PHP client for the Cloudflare Analytics GraphQL API.
โ ๏ธ Note: This package is not official and is not affiliated with Cloudflare. It is a community-driven package.
๐จ Note 2: This package is under development and is not ready for production.
You can install the package via composer:
composer require the-3labs-team/php-cloudflare-analytics
Add in your .env file the following variables:
CLOUDFLARE_API_TOKEN='your_cloudflare_api_token'
CLOUDFLARE_ZONE_TAG_ID='zoneTag'
or you can pass the token and zoneTag as parameters in the constructor.
use The3LabsTeam\PhpCloudflareAnalytics\CloudflareAnalytics;
$cf = new CloudflareAnalytics(
token: 'your_cloudflare_api_token', zoneTag: 'zoneTag'
);
You can use the following methods to build your query:
use The3LabsTeam\PhpCloudflareAnalytics\CloudflareAnalytics;
$cf = new CloudflareAnalytics;
$results = $cf->select('firewallEventsAdaptive AS firewall')
->get('firewall.datetime', 'firewall.action');
The get
method will return an array with the results.
firewallEventsAdaptive
httpRequests1mGroups
httpRequestsAdaptiveGroups
threatsAdaptiveGroups
threatsByCountryAdaptiveGroups
datetime
: 1 hourtake
: 10orderBy
:datetime
Get latest 10 firewall events:
$results = $cf->select('firewallEventsAdaptive AS firewall')
->get('firewall.datetime', 'firewall.action');
Filter between two dates:
$results = $cf->select('firewallEventsAdaptive AS firewall')
->where('firewall.datetime', '>=', '2021-10-01T00:00:00Z')
->where('firewall.datetime', '<=', '2021-10-02T00:00:00Z')
->get('firewall.datetime', 'firewall.action');
Limit the results:
$results = $cf->select('firewallEventsAdaptive AS firewall')
->take('firewall', 5)
->get('firewall.datetime', 'firewall.action');
Order the results:
$results = $cf->select('firewallEventsAdaptive AS firewall')
->orderBy('firewall.datetime', 'desc')
->get('firewall.datetime', 'firewall.action');
Get two fields from two different tables: // TODO: test this
$results = $cf->select('firewallEventsAdaptive AS firewall, threatsAdaptiveGroups AS threats')
->get('firewall.datetime', 'firewall.action', 'threats.datetime', 'threats.action');
Get http visits and sum them:
$results = $cf->select('httpRequests1mGroups AS http')
->take('http', 10)
->get('sum.countryMap.clientCountryName', 'sum.countryMap.requests', 'sum.countryMap.bytes', 'sum.countryMap.threats', 'dimensions.datetimeHour');
composer test
The MIT License (MIT). Please see License File for more information.
// TODO: Add the rest of the file