Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,44 @@
# featurevisor-example-php

Example PHP application using Featurevisor PHP SDK: https://github.com/featurevisor/featurevisor-php
A small PHP application showing how to use the [Featurevisor PHP SDK](https://github.com/featurevisor/featurevisor-php).

Learn more about Featurevisor: https://featurevisor.com
Learn more about Featurevisor [here](https://featurevisor.com).

## Installation
It fetches a production datafile, creates a Featurevisor instance with customer context, then evaluates a flag, a variation, feature variables, and global variables.

Clone the repository, and run this with Composer:
## Installation

```sh
composer install
```
$ composer install

## Run the example

```sh
php src/index.php
```

## Usage
The application evaluates `commerce_platform`, `checkout_experience`, `max_items`, `payment_methods`, `serviceEndpoints`, and `supportContact` from this datafile:

```text
https://featurevisor-example-cloudflare.pages.dev/production/featurevisor-sdk-v3.json
```
$ php src/index.php

Expected output:

```text
Commerce platform enabled: true
Checkout variation: express
Maximum checkout items: 25
Payment methods: [card, wallet]
Service endpoint: https://api.eu.example.com (timeout: 1200 ms, retries: 4)
Support contact: support-nl@example.com
```

Change the context in `src/index.php` to see how Featurevisor selects different rules, variations, and global variable overrides.

## Featurevisor project

Uses this Featurevisor project to fetch the configuration from: https://github.com/featurevisor/featurevisor-example-cloudflare
The datafile is generated by the [Featurevisor Cloudflare example](https://github.com/featurevisor/featurevisor-example-cloudflare).

Learn more in the [Featurevisor PHP SDK documentation](https://featurevisor.com/docs/sdks/php/).
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"type": "project",
"license": "MIT",
"require": {
"featurevisor/featurevisor-php": "^2.0"
"featurevisor/featurevisor-php": "^3.0"
}
}
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 50 additions & 16 deletions src/index.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,62 @@
<?php

declare(strict_types=1);

require_once __DIR__ . '/../vendor/autoload.php';

use Featurevisor\Featurevisor;

// fetch datafile
$DATAFILE_URL = "https://featurevisor-example-cloudflare.pages.dev/production/featurevisor-tag-all.json";
$datafileContent = json_decode(file_get_contents($DATAFILE_URL), true);
const DATAFILE_URL = 'https://featurevisor-example-cloudflare.pages.dev/production/featurevisor-sdk-v3.json';

// create instance
$f = Featurevisor::createFeaturevisor([
"datafile" => $datafileContent,
$requestContext = stream_context_create([
'http' => [
'header' => "Accept: application/json\r\n",
'timeout' => 10,
'ignore_errors' => true,
],
]);
$datafileJson = file_get_contents(DATAFILE_URL, false, $requestContext);

if ($datafileJson === false) {
throw new RuntimeException('Could not fetch the Featurevisor datafile.');
}

// set context
$f->setContext([
"userId" => "12345",
"deviceId" => "device-23456",
"country" => "nl",
$statusLine = $http_response_header[0] ?? '';
if (!preg_match('/\s2\d\d\s/', $statusLine)) {
throw new RuntimeException("Datafile request failed: {$statusLine}");
}

$datafile = json_decode($datafileJson, true, 512, JSON_THROW_ON_ERROR);
$f = Featurevisor::createFeaturevisor([
'datafile' => $datafile,
'logLevel' => 'error',
'context' => [
'userId' => 'customer-123',
'country' => 'nl',
'locale' => 'nl-NL',
'accountPlan' => 'pro',
],
]);

// evaluate values
if ($f->isEnabled("my_feature")) {
echo "Feature is enabled!";
} else {
echo "Feature is disabled.";
try {
$commerceEnabled = $f->isEnabled('commerce_platform');
$checkoutVariation = $f->getVariation('checkout_experience');
$maxItems = $f->getVariableInteger('checkout_experience', 'max_items');
$paymentMethods = $f->getVariableArray('checkout_experience', 'payment_methods');
$endpoints = $f->getVariableObject('serviceEndpoints');
$supportContact = $f->getVariableString('supportContact');

echo 'Commerce platform enabled: ' . ($commerceEnabled ? 'true' : 'false') . PHP_EOL;
echo 'Checkout variation: ' . ($checkoutVariation ?? 'unavailable') . PHP_EOL;
echo 'Maximum checkout items: ' . ($maxItems ?? 'unavailable') . PHP_EOL;
echo 'Payment methods: [' . implode(', ', $paymentMethods ?? []) . ']' . PHP_EOL;
printf(
"Service endpoint: %s (timeout: %d ms, retries: %d)\n",
$endpoints['baseUrl'],
$endpoints['timeoutMs'],
$endpoints['retries'],
);
echo 'Support contact: ' . ($supportContact ?? 'unavailable') . PHP_EOL;
} finally {
$f->close();
}