PHP SDK
Use Flipswitch with PHP via OpenFeature OFREP
Flipswitch works with PHP applications through the OpenFeature SDK and its native OFREP provider — no custom Flipswitch package needed.
GitHub: open-feature/php-sdk · open-feature/php-sdk-contrib
This integration uses the OpenFeature Remote Evaluation Protocol (OFREP), which evaluates flags via polling. Real-time SSE streaming is available in official Flipswitch SDKs.
Requirements
- PHP 8.1+
- Composer
Installation
composer require open-feature/sdk open-feature/ofrep-providerQuick Start
use OpenFeature\OpenFeatureAPI;
use OpenFeature\Contrib\Providers\OFREP\OFREPProvider;
$provider = new OFREPProvider([
'baseUrl' => 'https://api.flipswitch.io',
'headers' => [
'X-API-Key' => 'YOUR_API_KEY'
]
]);
$api = OpenFeatureAPI::getInstance();
$api->setProvider($provider);
$client = $api->getClient();
$darkMode = $client->getBooleanValue('dark-mode', false);Evaluation Context
Pass user attributes for targeting:
use OpenFeature\implementation\flags\EvaluationContext;
use OpenFeature\implementation\flags\Attributes;
$context = new EvaluationContext(
'user-123',
new Attributes([
'email' => 'user@example.com',
'plan' => 'premium',
'country' => 'SE'
])
);
$showFeature = $client->getBooleanValue('new-feature', false, $context);Detailed Evaluation
Get full evaluation details including variant and reason:
$details = $client->getBooleanDetails('feature-flag', false, $context);
echo "Value: " . ($details->getValue() ? 'true' : 'false') . "\n";
echo "Variant: " . $details->getVariant() . "\n";
echo "Reason: " . $details->getReason() . "\n";Laravel Integration
// app/Providers/FeatureFlagServiceProvider.php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use OpenFeature\OpenFeatureAPI;
use OpenFeature\Contrib\Providers\OFREP\OFREPProvider;
class FeatureFlagServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->app->singleton(OpenFeatureAPI::class, function () {
$provider = new OFREPProvider([
'baseUrl' => config('services.flipswitch.url'),
'headers' => [
'X-API-Key' => config('services.flipswitch.api_key')
]
]);
$api = OpenFeatureAPI::getInstance();
$api->setProvider($provider);
return $api;
});
}
}Use it in a controller:
// app/Http/Controllers/FeatureController.php
namespace App\Http\Controllers;
use OpenFeature\OpenFeatureAPI;
use OpenFeature\implementation\flags\EvaluationContext;
class FeatureController extends Controller
{
public function show(OpenFeatureAPI $api)
{
$client = $api->getClient();
$context = new EvaluationContext(auth()->id());
$enabled = $client->getBooleanValue('new-feature', false, $context);
return response()->json(['feature_enabled' => $enabled]);
}
}Direct OFREP API
You can also call the OFREP endpoints directly via HTTP — see the OFREP protocol reference for details.