|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +declare(strict_types=1); |
| 4 | + |
3 | 5 | require_once __DIR__ . '/../vendor/autoload.php'; |
4 | 6 |
|
5 | 7 | use Featurevisor\Featurevisor; |
6 | 8 |
|
7 | | -// fetch datafile |
8 | | -$DATAFILE_URL = "https://featurevisor-example-cloudflare.pages.dev/production/featurevisor-tag-all.json"; |
9 | | -$datafileContent = json_decode(file_get_contents($DATAFILE_URL), true); |
| 9 | +const DATAFILE_URL = 'https://featurevisor-example-cloudflare.pages.dev/production/featurevisor-sdk-v3.json'; |
10 | 10 |
|
11 | | -// create instance |
12 | | -$f = Featurevisor::createFeaturevisor([ |
13 | | - "datafile" => $datafileContent, |
| 11 | +$requestContext = stream_context_create([ |
| 12 | + 'http' => [ |
| 13 | + 'header' => "Accept: application/json\r\n", |
| 14 | + 'timeout' => 10, |
| 15 | + 'ignore_errors' => true, |
| 16 | + ], |
14 | 17 | ]); |
| 18 | +$datafileJson = file_get_contents(DATAFILE_URL, false, $requestContext); |
| 19 | + |
| 20 | +if ($datafileJson === false) { |
| 21 | + throw new RuntimeException('Could not fetch the Featurevisor datafile.'); |
| 22 | +} |
15 | 23 |
|
16 | | -// set context |
17 | | -$f->setContext([ |
18 | | - "userId" => "12345", |
19 | | - "deviceId" => "device-23456", |
20 | | - "country" => "nl", |
| 24 | +$statusLine = $http_response_header[0] ?? ''; |
| 25 | +if (!preg_match('/\s2\d\d\s/', $statusLine)) { |
| 26 | + throw new RuntimeException("Datafile request failed: {$statusLine}"); |
| 27 | +} |
| 28 | + |
| 29 | +$datafile = json_decode($datafileJson, true, 512, JSON_THROW_ON_ERROR); |
| 30 | +$f = Featurevisor::createFeaturevisor([ |
| 31 | + 'datafile' => $datafile, |
| 32 | + 'logLevel' => 'error', |
| 33 | + 'context' => [ |
| 34 | + 'userId' => 'customer-123', |
| 35 | + 'country' => 'nl', |
| 36 | + 'locale' => 'nl-NL', |
| 37 | + 'accountPlan' => 'pro', |
| 38 | + ], |
21 | 39 | ]); |
22 | 40 |
|
23 | | -// evaluate values |
24 | | -if ($f->isEnabled("my_feature")) { |
25 | | - echo "Feature is enabled!"; |
26 | | -} else { |
27 | | - echo "Feature is disabled."; |
| 41 | +try { |
| 42 | + $commerceEnabled = $f->isEnabled('commerce_platform'); |
| 43 | + $checkoutVariation = $f->getVariation('checkout_experience'); |
| 44 | + $maxItems = $f->getVariableInteger('checkout_experience', 'max_items'); |
| 45 | + $paymentMethods = $f->getVariableArray('checkout_experience', 'payment_methods'); |
| 46 | + $endpoints = $f->getVariableObject('serviceEndpoints'); |
| 47 | + $supportContact = $f->getVariableString('supportContact'); |
| 48 | + |
| 49 | + echo 'Commerce platform enabled: ' . ($commerceEnabled ? 'true' : 'false') . PHP_EOL; |
| 50 | + echo 'Checkout variation: ' . ($checkoutVariation ?? 'unavailable') . PHP_EOL; |
| 51 | + echo 'Maximum checkout items: ' . ($maxItems ?? 'unavailable') . PHP_EOL; |
| 52 | + echo 'Payment methods: [' . implode(', ', $paymentMethods ?? []) . ']' . PHP_EOL; |
| 53 | + printf( |
| 54 | + "Service endpoint: %s (timeout: %d ms, retries: %d)\n", |
| 55 | + $endpoints['baseUrl'], |
| 56 | + $endpoints['timeoutMs'], |
| 57 | + $endpoints['retries'], |
| 58 | + ); |
| 59 | + echo 'Support contact: ' . ($supportContact ?? 'unavailable') . PHP_EOL; |
| 60 | +} finally { |
| 61 | + $f->close(); |
28 | 62 | } |
0 commit comments