• HOME
  • INTEGRATIONS
  • SDKS
  • GUIDES
  • API DOCS
No results for ""
EXPAND ALL
launchdarkly.com

EDIT ON GITHUB

PHP SDK reference

Read time: 2 minutes
Last edited: Jun 18, 2021

This reference guide documents all of the methods available in the PHP SDK, and explains in detail how these methods work. If you want to dig even deeper, our SDKs are open source. To learn more, read PHP SDK repository on GitHub. The online API docs contain the programmatic definitions of every type and method. Additionally you can clone and run a sample application using this SDK.

Requirements
PHP 5.5 or higher.

Getting started

Building on top of our Getting Started guide, the following steps will get you started with using the LaunchDarkly SDK in your PHP application.

The first step is to install Composer and the LaunchDarkly SDK as a dependency in your application. Refer to the SDK releases page to identify the latest version if you want to depend on a specific version.

1php composer.phar require launchdarkly/server-sdk
2
3# Note that in earlier versions, this was "launchdarkly/launchdarkly-php"

Then require Composer's autoloader.

1require 'vendor/autoload.php';

Once the SDK is installed, you'll want to create a single, shared instance of LDClient. You should specify your SDK key here so that your application will be authorized to connect to LaunchDarkly and for your application and environment.

1$client = new LaunchDarkly\LDClient("YOUR_SDK_KEY");

Using $client, you can check which variation a particular user should receive for a given feature flag.

1$user = new LaunchDarkly\LDUser("user@test.com");
2
3if ($client->variation("your.flag.key", $user)) {
4 // application code to show the feature
5} else {
6 // the code to run if the feature is off
7}

In our Getting Started guide we recommend that users shut down the LaunchDarkly client on application termination. This step does not exist in PHP because the PHP SDK does not maintain long-lived network connections nor an event queue.

Fetching flags

There are two distinct methods of integrating LaunchDarkly in a PHP environment:

  • The Relay Proxy retrieves and store flags in Redis (recommended)
  • Guzzle Cache Middleware requests and caches HTTP responses in an in-memory array (default)

We strongly suggest using the Relay Proxy. Per-flag caching mode using Guzzle is only intended for low-throughput environments.

Using the Relay Proxy

PHP's shared-nothing architecture prevents LaunchDarkly from re-using the streaming API connection across requests.

You can use PHP without the Relay Proxy, but we strongly recommend using the Relay Proxy in daemon mode if you are using PHP in a high-throughput setting. This makes the Relay Proxy receive feature flag updates.

To learn more, read Using the Relay Proxy in different modes.

Using Guzzle

Require Guzzle as a dependency:

1php composer.phar require "guzzlehttp/guzzle:6.2.1"
2php composer.phar require "kevinrob/guzzle-cache-middleware:1.4.1"

It will then be used as the default way of fetching flags.

With Guzzle, you could persist your cache somewhere other than the default in-memory store, like Memcached or Redis.

You could then specify your cache when initializing the client with the cache option.

1$client = new LaunchDarkly\LDClient("YOUR_SDK_KEY", array("cache" => $cacheStorage));

Supported features

This SDK supports the following features: