Download the PHP package greenflags/greenflags-php without Composer
On this page you can find all versions of the php package greenflags/greenflags-php. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download greenflags/greenflags-php
More information about greenflags/greenflags-php
Files in greenflags/greenflags-php
Package greenflags-php
Short Description PHP SDK for GreenFlags feature flags: cached snapshot reads, geofence evaluation. Zero dependencies, billing-safe for PHP-FPM via pluggable snapshot cache.
License MIT
Homepage https://greenflags.dev
Informations about the package greenflags-php
greenflags/greenflags-php
Official PHP SDK for consuming GreenFlags feature flags from any PHP application: Laravel, Symfony, WordPress plugins, plain PHP.
Zero dependencies — PHP standard library only (streams + JSON). Built to minimize billable requests even in request-scoped PHP-FPM, via a pluggable snapshot cache: at most one network read per TTL per server, no matter how many requests you handle.
Status:
0.1.0. Full changelog inCHANGELOG.md.
Table of Contents
- Why it exists
- The PHP-FPM problem (and the fix)
- Features
- Requirements
- Quick Start
- Usage Guide
- Laravel Recipe
- WordPress / WooCommerce Recipe
- API Reference
- Geofence
- Error Handling
- Billing Model
- Development
- Versioning
Why it exists
GreenFlags exposes a read endpoint (GET /v1/flags) where every 2xx response counts as a billable read. A naive integration that fetches flags on every request can generate thousands of unnecessary reads.
The PHP-FPM problem (and the fix)
In Node/Go/Python, an in-memory snapshot survives between requests. Classic PHP-FPM tears the process down after every request — in-memory caching alone would still pay one read per request.
This SDK fixes that with a SnapshotCacheInterface (two methods: get/set a JSON string with a TTL) plus the sync() method:
sync(60)checks the cache. Fresh snapshot there? Zero network calls.- Cache empty or expired? One
GET /v1/flags(1 billable read), stored back with the TTL.
Result: at most one read per minute per server (with ttl=60), whether you serve 10 or 10,000 requests. Adapters included: ApcuCache (shared across FPM workers) and InMemoryCache (Octane/workers/tests). A Redis or Laravel Cache adapter is ~5 lines (see the recipe below).
Features
- ✅ Zero dependencies — Composer installs nothing else.
- ✅ Billing-safe in PHP-FPM via the snapshot cache +
sync(). - ✅ Fail-open — a failed refresh keeps the last loaded snapshot;
getFlagreturns your default for missing flags. - ✅ Client-side geofence evaluation — the end-user's location never leaves your server.
- ✅ Injectable HTTP layer — trivial to test, or to swap streams for Guzzle/cURL.
- ✅ PHP 8.1+,
declare(strict_types=1), readonly value objects.
Requirements
- PHP 8.1+ with
ext-json. - A GreenFlags API token, generated from the dashboard for a specific
project + environment. See the API docs for the full contract.
Quick Start
Usage Guide
Ground rules
getFlag/isEnablednever throw for missing flags —getFlagreturns your default andisEnabledreturnsfalse.sync()/refresh()can throwGreenFlagsException— butsync()swallows network failures when it already has flags to serve (fail-open for long-running runtimes).- Pick your cache by deployment:
ApcuCachefor classic FPM,InMemoryCachefor Octane/queues/CLI, or write a Redis/Laravel adapter for multi-server fleets (one read per TTL for the whole fleet).
Laravel Recipe
With Redis as Laravel's cache driver, your entire fleet shares one snapshot: one billable read per minute total.
WordPress / WooCommerce Recipe
WordPress ships its own cache primitive — transients — which maps directly onto SnapshotCacheInterface. Drop this in a small plugin (or wp-content/mu-plugins/greenflags.php):
Use it anywhere in your theme or WooCommerce hooks:
Or expose a shortcode so editors can gate content from the block editor:
Define the token in wp-config.php (never in the database or a template):
With an object cache plugin (Redis/Memcached), transients are shared across all PHP workers — one billable read per minute for the whole site.
API Reference
new Client(...)
| Parameter | Type | Description |
|---|---|---|
url |
string |
API base URL, trailing slash optional. |
apiToken |
string |
Environment-scoped token (gf_...). |
coordinates |
?Coordinates |
Optional end-user location for geofence evaluation. |
cache |
?SnapshotCacheInterface |
Optional snapshot cache — strongly recommended in PHP-FPM. |
httpGet |
?callable |
Optional HTTP layer: (string $url, array $headers): array{int, string}. |
Methods
| Method | Signature | Description |
|---|---|---|
sync |
(int $ttlSeconds = 60): void |
Cache-first load: 0 requests on hit, 1 billable read on miss (stored back with the TTL). |
refresh |
(int $cacheTtlSeconds = 60): void |
Forces 1 request, replaces the snapshot, updates the cache. |
getSnapshot |
(): array<string, Flag> |
Evaluated snapshot, keyed by flag key. Local read. |
getAllFlags |
(): list<Flag> |
Every evaluated flag. Local read. |
getFlag |
(string $key, mixed $default = null): mixed |
Evaluated value or $default. Never throws for missing flags. |
isEnabled |
(string $key): bool |
true only when the flag exists and evaluates to true. |
setCoordinates |
(?Coordinates): void |
Sets/clears the location for geofence evaluation. No network. |
Geofence
Flags may carry a geofence (latitude/longitude/radius, configured in the dashboard). With coordinates set, each geofenced flag is evaluated locally:
- Inside the radius (on-edge counts as inside): the flag's normal value.
- Outside: the off value —
falseforbooleanflags,nullfor the rest. - No coordinates, or no geofence: the normal value, unaffected.
Fail-open, by design: a geofence is not a security boundary.
Error Handling
errorCode |
status |
Cause |
|---|---|---|
INVALID_TOKEN |
401 | Token missing, invalid, or revoked |
QUOTA_EXCEEDED |
429 | Monthly read quota exhausted |
BILLING_NO_SUBSCRIPTION |
429 | The workspace has no active subscription |
BILLING_CANCELED |
429 | Subscription canceled |
BILLING_PAST_DUE |
429 | Payment past due |
BILLING_TRIAL_EXPIRED |
429 | Trial expired |
BILLING_LIMIT_REACHED |
429 | Billing limit reached |
NETWORK_ERROR |
0 | The request failed before a response was received |
PARSE_ERROR |
response status | Body wasn't valid JSON, or was missing data.flags |
REQUEST_ERROR |
response status | Non-2xx response with no parseable error code |
Read methods never throw any of these — they're always local.
Billing Model
Only refresh() — or a sync() cache miss — makes a request; every 2xx counts as one billable read. With sync(60) + a shared cache (APCu per server, Redis per fleet), your cost ceiling is 1 read per TTL per cache, independent of traffic.
Development
Versioning
Semver, while in 0.x: MINOR can include API changes, PATCH are fixes. Version-by-version detail in CHANGELOG.md.
Related
- API reference
@greenflags/client— JavaScript/TypeScript SDK@greenflags/react/@greenflags/vue— framework bindingsgreenflags— Dart/Flutter SDKgreenflagson PyPI — Python SDKgreenflags-go— Go SDK@greenflags/mcp— MCP server for AI agents
Percentage rollout & variants
Server processes serve many users, so identity is per call — resolve rollout/variants for a specific end user with:
getFlag (no user) keeps returning the raw value (fail-open). Bucketing is deterministic and identical across every GreenFlags SDK and the server (docs/rollout-hash-spec.md).
All versions of greenflags-php with dependencies
ext-json Version *