diff --git a/README.md b/README.md index 927905e..65e7bb9 100644 --- a/README.md +++ b/README.md @@ -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/). diff --git a/composer.json b/composer.json index cd6f112..72ebc23 100644 --- a/composer.json +++ b/composer.json @@ -4,6 +4,6 @@ "type": "project", "license": "MIT", "require": { - "featurevisor/featurevisor-php": "^2.0" + "featurevisor/featurevisor-php": "^3.0" } } diff --git a/composer.lock b/composer.lock index a8ae55d..2231d09 100644 --- a/composer.lock +++ b/composer.lock @@ -4,20 +4,20 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "9b40ada326ff79a1d8fd2329f61a3cd5", + "content-hash": "75894dbcaa0704ac1816370c9a5afc5c", "packages": [ { "name": "featurevisor/featurevisor-php", - "version": "v2.0.0", + "version": "v3.0.0", "source": { "type": "git", "url": "https://github.com/featurevisor/featurevisor-php.git", - "reference": "bfd61c573892e5f7cdbaedf97757846392be4454" + "reference": "d36b58a9920f8053b82ee9cdfe8d2bb6bd3ec46c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/featurevisor/featurevisor-php/zipball/bfd61c573892e5f7cdbaedf97757846392be4454", - "reference": "bfd61c573892e5f7cdbaedf97757846392be4454", + "url": "https://api.github.com/repos/featurevisor/featurevisor-php/zipball/d36b58a9920f8053b82ee9cdfe8d2bb6bd3ec46c", + "reference": "d36b58a9920f8053b82ee9cdfe8d2bb6bd3ec46c", "shasum": "" }, "require": { @@ -66,7 +66,7 @@ "issues": "https://github.com/featurevisor/featurevisor-php/issues", "source": "https://github.com/featurevisor/featurevisor-php" }, - "time": "2026-07-23T22:41:41+00:00" + "time": "2026-08-29T15:22:03+00:00" } ], "packages-dev": [], diff --git a/src/index.php b/src/index.php index 624d0ca..51cc1cc 100644 --- a/src/index.php +++ b/src/index.php @@ -1,28 +1,62 @@ $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(); }