初始化开发工具包

云眼About 5 min

初始化开发工具包

本主题介绍如何在应用程序中初始化云眼灰度发布(特性标帜)AB实验 PHP SDK。

使用该方法初始化 PHP SDK,并实例化公开 API 方法(如 Determined 方法)的 Eyeofcloud 客户端类的实例。每个客户端对应于表示特定环境的项目状态的数据文件。数据文件可通过 EyeofcloudConfig 访问。instantiate

版本

SDK v3.1.0 及更高版本

描述

构造函数接受配置对象来配置云眼灰度发布(特性标帜)AB实验。

某些参数是可选的,因为 SDK 提供默认实现,但开发者可能希望为生产环境覆盖这些参数。例如,开发者可能希望设置错误处理程序记录器来捕获问题,设置事件调度程序来管理网络调用,以及用户配置文件服务以确保粘性分桶。

参数

下表列出了 PHP 中该方法的必需参数和可选参数(客户端使用 Builder 类构造并传递给):Build()``start

参数

类型

描述

数据文件
可选

字符串

表示项目的 JSON 字符串。

错误处理程序
可选

IErrorHandler

用于处理错误的错误处理程序对象。

access_token_可选_

字符串

(仅限服务器端)云眼灰度发布(特性标帜)AB实验 SDK 可以使用访问令牌(与 SDK 密钥结合使用)从经过身份验证的终结点提取数据文件。在 Eyeofcloud 应用程序的设置>环境中找到数据文件访问令牌。选择安全环境,然后复制_数据文件访问令牌_。

返回

实例化 Eyeofcloud 类的实例。

自动数据文件管理 (ADM)

云眼灰度发布(特性标帜)AB实验提供了开箱即用的功能,用于动态管理客户端或服务器上的数据文件(配置文件)。PHP SDK 提供 ProjectConfigManagerInterfaceopen in new window 的默认实现。

可以使用 EyeofcloudFactory 方法通过 SDK 密钥和可选的回退数据文件创建 Eyeofcloud 客户端。使用此方法在内部创建一个 HTTPProjectConfigManager。有关更多详细信息,请参阅 HttpProjectConfigManager

仅使用 SDK 密钥的基本示例

.PHP

$eyeofcloudClient = EyeofcloudFactory::createDefaultInstance($sdkKey); // access your HTTPProjectConfigManager $configManager = $eyeofcloudClient->configManager;

可以使用 HttpProjectConfigManager 的默认配置实例化 Eyeofcloud灰度发布(特性标帜) PHP SDK。

使用 HTTPProjectConfigManager 的基本示例

.PHP

$configManager = new HTTPProjectConfigManager($sdkKey); $eyeofcloud = new Eyeofcloud( $datafile, null, null, null, false, null, $configManager, $notificationCenter );

ProjectConfigManagerInterfaceopen in new window exposes the method for retrieving the ProjectConfig instance.getConfig

HTTPProjectConfigManager

HTTPProjectConfigManageropen in new window is an implementation of ProjectConfigManagerInterfaceopen in new window interface.

The method makes a blocking HTTP GET request to the configured URL to download the project datafile and initialize an instance of the .fetch``ProjectConfig

Advanced example

PHP

$configManager = new HTTPProjectConfigManager( $sdkKey, $url, $urlTemplate, $fetchOnInit, $datafile ); $eyeofcloud = new Eyeofcloud( $datafile, null, null, null, false, null, $configManager, $notificationCenter );

The method allows you to refresh the . To update the , you can do something like:fetch``ProjectConfig``ProjectConfig

PHP

// Call fetch to retrieve an up-to-date-datafile. $configManager->fetch(); // Now the Eyeofcloud instance has the latest version of the datafile.

SDK key

The SDK key is used to compose the outbound HTTP request to the default datafile location on the Eyeofcloud CDN.

Parameters

The table below lists the required and optional parameters in PHP.

Parameter

Default

Description

sdkKey

null

Eyeofcloud 灰度发布(特性标帜) project SDK key; required unless source URL is overridden.

url

null

URL override location used to specify custom HTTP source for the Eyeofcloud Feature Experimentation datafile.

urlTemplate

null

Parameterized datafile URL by SDK key.

fetchOnInit

false

Boolean flag to specify if datafile fetching should start immediately, as soon as the HTTPConfigManager is initialized.

datafile

null

Initial datafile, typically sourced from a local cached source.

Update config notifications

A notification will be triggered whenever a new datafile is fetched and is updated. To subscribe to these notifications, use the .ProjectConfig``$notificationCenter->addNotificationListener();

PHP

$notificationCenter->addNotificationListener( NotificationType::EYEOFCLOUD_CONFIG_UPDATE, $updateCallback );

Example

You can instantiate with a hard-coded datafile. If you do not pass in an SDK key, the Eyeofcloud Client will not automatically sync newer versions of the datafile. Any time you retrieve an updated datafile, just re-instantiate the same client.

For simple applications, all you need to provide to instantiate a client is a datafile specifying the project configuration for a given environment. For most advanced implementations, you will want to customize the logger or error handler for your specific requirements.

PHP

$template = "https://cdn.eyeofcloud.com/datafiles/%s.json"; $configManager = new HTTPProjectConfigManager('QBw9gFM8oTn7ogY9ANCC1z', null, $template, true, null, false, null, null); $eyeofcloudClient = new Eyeofcloud($datafile, null, null, null, false, null, $configManager);

Notes

Enable JSON schema validation

跳过 JSON 架构验证可提高实例化期间的性能。在 PHP SDK 中,可以控制在实例化客户端时是否验证数据文件的 JSON 架构。此示例演示如何跳过 JSON 架构验证:

.PHP

// Skip JSON schema validation $eyeofcloudClient = new Eyeofcloud($datafile);

在安全环境中使用经过身份验证的数据文件

📘 注意

经过身份验证的数据文件处于测试阶段。如果有兴趣成为经过身份验证的数据文件的早期用户,作为测试版安全环境功能的一部分,请联系客户成功经理。

可以使用服务器端(仅限)云眼 SDK 从经过身份验证的端点获取 Eyeofcloud 数据文件。要使用经过身份验证的数据文件,请从设置>环境中的 Eyeofcloud 应用程序下载 Eyeofcloud 环境的访问令牌。选择安全环境,然后复制_数据文件访问令牌_。下面的示例显示了如何使用访问令牌和 sdkKey 初始化 Eyeofcloud 客户端,使客户端能够获取经过身份验证的数据文件并完成初始化。

.PHP

// fetch the datafile from an authenticated endpoint use Eyeofcloud\EyeofcloudFactory; $sdkKey = '<YOUR_SDK_KEY>'; $datafileAuthToken = '<YOUR_DATAFILE_ACCESS_TOKEN>'; $eoc = EyeofcloudFactory::createDefaultInstance($sdkKey, null, $datafileAuthToken);

源文件

包含 PHP 实现的语言/平台源文件位于 Eyeofcloud.phpopen in new window

Last update:
Contributors: “zhangweixue”