the-3labs-team / php-cloudflare-analytics
A php package to retrieve page views and other data from Cloudflare Analytics
Fund package maintenance!
The3LabsTeam
Requires
- php: ^8.1
- vlucas/phpdotenv: ^5.6
Requires (Dev)
- laravel/pint: ^1.0
- pestphp/pest: ^2.20
- spatie/ray: ^1.28
This package is auto-updated.
Last update: 2024-10-28 19:08:24 UTC
README
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.
Installation
You can install the package via composer:
composer require the-3labs-team/php-cloudflare-analytics
Configuration
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' );
Usage
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.
Available fields
firewallEventsAdaptive
httpRequests1mGroups
httpRequestsAdaptiveGroups
threatsAdaptiveGroups
threatsByCountryAdaptiveGroups
Default fields
datetime
: 1 hourtake
: 10orderBy
:datetime
Demo
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');
Testing
composer test
License
The MIT License (MIT). Please see License File for more information.
Credits
// TODO: Add the rest of the file