PHP SDK Reference
This topic describes how to use the Harness Feature Flags PHP SDK for your PHP application. For getting started quickly, you can use our sample code from the PHP SDK README. You can also clone and ru…
This topic describes how to use the Harness Feature Flags PHP SDK for your PHP application.
For getting started quickly, you can use our sample code from the PHP SDK README. You can also clone and run a sample application from the PHP SDK GitHub Repository.
Important: The PHP SDK Github repository was previously known as ff-php-server-sdk it has since been renamed to ff-php-client-sdk. This is to make it clearer that the SDK requires a client SDK key. The published package is still named ff-server-sdk this will change in a future release.
Before You Begin
Make sure you've read and understood:
Version
Latest SDK version can be found on GitHub Release Page
Requirements
To use this SDK, make sure you:
Install PHP version 7.4 or newer
Install Composer
Create a PHP application, or clone our sample application.
Create a Feature Flag on the Harness Platform. If you are following along with the SDK README sample code, make sure your flag is called
harnessappdemodarkmode.
Install the SDK
Use Composer to install the SDK as a dependency in your application, for example:
Initialize the SDK
To initialize the PHP SDK, you need to:
Add your Client SDK key to connect to your Harness Environment.
Add a Target that you want to Evaluate against a Feature Flag.
(Optional) Configure the SDK.
Complete the initialization using the Client SDK Key, Target, and any Configuration parameters you set.
Add the Client SDK Key
To connect to the correct Environment that you set up on the Harness Platform, you need to add the Client SDK Key from that Environment.
Input the Client SDK Key into the SDK_KEY parameter, for example:
Add a Target
For more information about Targets, go to Targeting Users With Flags.
To add a Target, build it and pass in arguments for the following:
Parameter
Description
Required?
Example
identifier
Unique ID for the Target.Read Regex requirements for Target names and identifiers below for accepted characters.
Required
"identifier" => "HT_1"
name
Name for this Target. This does not have to be unique. Note: If you don’t provide a value, the name will be the same as the identifier.Read Regex requirements for Target names and identifiers below for accepted characters.
OptionalNote: If you don't want to send a name, don't send the parameter. Sending an empty argument will cause an error.
“name" => "Harness_Target_1",
attributes
Additional data you can store for a Target, such as email addresses or location.
Optional
“attributes" => [“email” => “sample@sample.com”]
Configure the SDK
When initializing the SDK, you also have the option of providing alternative configuration by passing in the configuration options to the cfClient.
You can configure the following base features of the SDK:
Name
Description
Default Value
base_url
The URL used to fetch Feature Flag Evaluations. When using the Relay Proxy, change this to: http://localhost:7000
No default, provide your own value.
events_url
The URL for posting metrics data to the Feature Flag service. When using the Relay Proxy, change this to: http://localhost:7000
No default, provide your own value.
expireAfter
The amount of time in seconds that data is removed from the cache.
60 (seconds)
metricsEnabled
Set to true to enable analytics.Set to false to disable analytics.Note: When enabled, analytics data is posted every 60 seconds.
true
For example:
Complete the Initialization
To complete the initialization, create an instance of the Client and pass in the SDK_Key and Target, for example:
Sample of Initializing the SDK
Evaluate a Flag
Evaluating a Flag is when the SDK processes all Flag rules and returns the correct Variation of that Flag for the Target you provide.
If a matching Flag can’t be found, or the SDK can’t remotely fetch flags, the default value is returned.
There are different methods for the different Variation types and for each method you need to pass in:
Identifier of the Flag you want to evaluate
The default Variation
For example:
Test Your App is Connected to Harness
When you receive a response showing the current status of your Feature Flag, go to the Harness Platform and toggle the Flag on and off. Then, check your app to verify if the Flag Variation displayed is updated with the Variation you toggled.
Close the SDK client
In most applications, you won't need to close the SDK client.
However, you should close the SDK client if:
Your application is about to terminate. Closing the client ensures that all associated resources are released.
You have determined that you do not need to evaluate flags again in your application lifecycle.
Sample Code for a PHP Application
Here is a sample code for integrating with the PHP SDK:
Last updated
Was this helpful?