webmarketingroi/optimizely-php
Composer 安装命令:
composer require webmarketingroi/optimizely-php
包简介
A PHP wrapper around Optimizely REST API
关键字:
README 文档
README
A PHP wrapper library for the Optimizely REST API v2.0 (https://developers.optimizely.com/rest/v2/), proudly created and open sourced by Optimizely Solutions Partner, Web Marketing ROI.
Installation
This library requires at least version 5.6 of PHP with the curl extension installed.
Installing with Composer:
php composer.phar require webmarketingroi/optimizely-php
The command above will install the latest stable version.
Or, if you prefer a bleeding-edge version, install dev-master with the following
command:
php composer.phar require webmarketingroi/optimizely-php dev-master
Usage
First, you need to create an instance of the OptimizelyApiClient class. You can do that
using the following lines of code:
<?php use WebMarketingROI\OptimizelyPHP\OptimizelyApiClient; use WebMarketingROI\OptimizelyPHP\Exception; try { // If you use the "OAuth 2.0 authorization code" grant, use the following array. $authCredentials = array( 'client_id' => 'YOUR_CLIENT_ID', 'client_secret' => 'YOUR_CLIENT_SECRET', 'refresh_token' => 'YOUR_REFRESH_TOKEN', // Access token is optional (if not provided, will be retrieved automatically // with the refresh token). 'access_token' => 'YOUR_ACCESS_TOKEN' ); // Or, if you use the "OAuth 2.0 implicit grant" or "Optimizely personal // token", use the following array. Please note that personal tokens are not // recommended to use in production environments. $authCredentials = array( 'access_token' => 'YOUR_ACCESS_TOKEN' ); // Instantiate the API client. $client = new OptimizelyApiClient($authCredentials, 'v2'); // Do something with the client (for example, get the projects). $result = $client->projects()->listAll(); // Extract projects from result. $projects = $result->getPayload(); foreach ($projects as $project) { // Get project attributes. $name = $project->getName(); echo "Name: $name\n"; } // Finally, retrieve the access token from the client (this is only required // if you use "OAuth 2.0 authorization code" grant). $accessToken = $client->getAccessToken(); // Save the access token somewhere (to a file or database) for later use. file_put_contents('access_token.json', json_encode($accessToken)); } catch (Exception $e) { // Handle error. $code = $e->getCode(); // Client-specific error code. $httpCode = $e->getHttpCode(); // HTTP status code. $message = $e->getMessage(); // Error message. $uuid = $e->getUuid(); // Error UUID. echo "Exception caught: $message (code=$code http_code=$httpCode uuid=$uuid)\n"; }
The first argument of the OptimizelyApiClient constructor should be your Optimizely
API credentials in the form of an array, the second argument represents the API version
(currently, only 'v2' is supported).
Note: For information on how to get OAuth 2.0 credentials or a personal token, please refer to Optimizely's documentation https://developers.optimizely.com/x/authentication/oauth/.
Working with Projects
Getting List of Projects
Use the following code to retrieve all Optimizely projects:
$page = 1; for (;;) { // Get the next page of projects. $result = $client->projects()->listAll($page, 25); // Retrieve projects from Result object. $projects = $result->getPayload(); // Iterate through retrieved projects foreach ($projects as $project) { echo "ID: " . $project->getId() . "\n"; echo "Name: " . $project->getName() . "\n"; echo "Account ID: " . $project->getAccountId() . "\n"; echo "Platform: " . $project->getPlatform() . "\n"; echo "Status: " . $project->getStatus() . "\n"; echo "Is Classic: " . ($project->getIsClassic()?"true":"false") . "\n"; echo "Created: " . $project->getCreated() . "\n"; echo "Last Modified: " . $project->getLastModified() . "\n"; echo "\n"; } // Determine if there are more projects. if ($result->getNextPage()==null) break; // Increment page counter. $page ++; }
Adding New Project
To add a new project, use the following code:
<?php use WebMarketingROI\OptimizelyPHP\Resource\v2\Project; $project = new Project(); $project->setName('Test Project'); $project->setConfidenceThreshold(0.9); $project->setPlatform('web'); $project->setStatus('active'); // On return, call $result->getPayload() to get the newly created project $result = $client->projects()->create($project); $createdProject = $result->getPayload();
or, you can use this (equivalent) code:
<?php use WebMarketingROI\OptimizelyPHP\Resource\v2\Project; $result = new Project(array( 'name' => 'Test Project', 'confidence_threshold' => 0.9, 'platform' => 'web', 'status' => 'active' )); // On return, call $result->getPayload() to get the newly created project $result = $client->projects()->create($project); $createdProject = $result->getPayload();
Updating Existing Project
<?php // We assume that $project is of type Project and that you retrieved it earlier $project->setName('New Project Name'); // On return, call $result->getPayload() to get the data of the updated project $result = $client->projects()->update($project); $updatedProject = $result->getPayload();
Getting the List of Running Campaigns
$page = 1; for (;;) { // Get the next page of Campaigns. $result = $client->campaigns()->listAll($page, 25); // Retrieve Campaigns from Result object. $campaigns = $result->getPayload(); // Iterate through retrieved Campaigns foreach ($campaigns as $campaign) { if ($campaign->getStatus()!='active') continue; echo "Name: " . $campaign->getName() . "\n"; echo "Type: " . $campaign->getType() . "\n"; echo "Status: " . $campaign->getStatus() . "\n"; echo "\n"; } // Determine if there are more Campaigns. if ($result->getNextPage()==null) break; // Increment page counter. $page ++; }
Adding a New Campaign
<?php use WebMarketingROI\OptimizelyPHP\Resource\v2\Campaign; $campaign = new Campaign(); $campaign->setName('Test Campaign'); $campaign->setType('a/b'); $campaign->setStatus('active'); // On return, call $result->getPayload() to get the newly created Campaign $result = $client->campaigns()->create($campaign); $createdCampaign = $result->getPayload();
More Code Examples
For additional code examples, please refer to the examples directory.
Running Unit Tests
This library uses PHPUnit for testing. To run unit tests, use the following command:
./vendor/bin/phpunit -c ./tests/phpunit.xml --coverage-html coverage
If you want to run integration tests against a real Optimizely account, rename
tests/auth_credentials.json.dist to tests/auth_credentials.json and type
your credentials in that file. Then, create the OPTIMIZELY_PHP_TEST_INTEGRATION
environment variable as follows
export OPTIMIZELY_PHP_TEST_INTEGRATION=1
and then run unit tests.
Open source projects using optimizely-php
- optimizelyBillingUnpauser. This is a simple script that checks if none of your optimizely experiments are running (presumably due to passing your payment limit for the month) and then it resumes the latest ones to be paused (assuming those are the ones optimizely paused for you).
webmarketingroi/optimizely-php 适用场景与选型建议
webmarketingroi/optimizely-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 395 次下载、GitHub Stars 达 3, 最近一次更新时间为 2016 年 10 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「optimizely」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 webmarketingroi/optimizely-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webmarketingroi/optimizely-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 webmarketingroi/optimizely-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Optimizely PHP SDK for Optimizely Feature Experimentation, Optimizely Full Stack (legacy), and Optimizely Rollouts
Optimizely SDK for Full Stack PHP projects.
An Optimizely app for Strata
A server-side A/B testing tool for Laravel 5.
A server-side A/B/n testing tool
A/B testing tool for Laravel apps
统计信息
- 总下载量: 395
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-10-13