tdaws/3scale_ws_api_for_php

本包最新版(dev-master)没有提供许可信息。

PHP应用程序的3scale集成插件 - 带有composer.json

dev-master 2013-08-28 15:58 UTC

This package is not auto-updated.

Last update: 2024-09-14 12:24:08 UTC


README

3Scale已经将composer.json添加到他们的仓库中,因此这个包不再必要。请切换到使用他们的包:"3scale/3scale_ws_api_for_php": "dev-master"。

我将不再更新这个包。

  • Tony Daws

3scale Web服务管理系统API客户端 构建状态

3scale集成插件用于PHP应用程序。3scale是一个API基础设施服务,处理API密钥、速率限制、分析、账单支付和开发者管理。包括可配置的API仪表板和开发者门户CMS。更多产品信息请访问http://www.3scale.net/,支持信息请访问http://support.3scale.net/

安装

从github下载源代码:http://github.com/3scale/3scale_ws_api_for_php 并将其放置在项目可访问的位置。

用法

需要ThreeScaleClient.php文件(假设您已经将库放置在include路径内的某个位置)

require_once('lib/ThreeScaleClient.php')

然后,创建客户端实例,传入您的提供者API密钥

$client = new ThreeScaleClient("your provider key");

由于对象是无状态的,您可以创建一个并存储在全局范围内。

授权

要授权特定应用程序,调用authorize方法,传入应用程序ID和可选的应用程序密钥

$response = $client->authorize("the app id", "the app key");

然后调用返回对象的isSuccess()方法以查看授权是否成功

if ($response->isSuccess()) {
  // All fine, proceeed.
} else {
  // Something's wrong with this app.
}

如果提供者和应用程序ID都有效,则响应对象包含有关应用程序状态的其他信息

// Returns the name of the plan the application is signed up to.
$response->getPlan()

如果计划定义了使用限制,则响应包含按指标和使用限制周期拆分的详细信息。

// The usageReports array contains one element per each usage limit defined on the plan.
$usageReports = $response->getUsageReports();
$usageReport  = $usageReports[0];

// The metric
$usageReport->getMetric() // "hits"

// The period the limit applies to
$usageReport->getPeriod()       // "day"
$usageReport->getPeriodStart()  // 1272405600 (Unix timestamp for April 28, 2010, 00:00:00)
$usageReport->getPeriodEnd()    // 1272492000 (Unix timestamp for April 29, 2010, 00:00:00)

// The current value the application already consumed in the period
$usageReport->getCurrentValue() // 8032

// The maximal value allowed by the limit in the period
$usageReport->getMaxValue()     // 10000

// If the limit is exceeded, this will be true, otherwise false:
$usageReport->isExceeded()      // false

如果授权失败,则getErrorCode()返回系统错误代码和getErrorMessage()可读的错误描述

$response->getErrorCode()       // "usage_limits_exceeded"
$response->getErrorMessage()    // "Usage limits are exceeded"

报告

要报告使用情况,使用report方法。您可以同时报告多个事务

$response = $client->report(array(
  array('app_id' => "first app's id",  'usage' => array('hits' => 1)),
  array('app_id' => "second app's id", 'usage' => array('hits' => 1))));

需要"app_id""usage"参数。另外,您可以指定事务的时间戳

$response = $client->report(array(
  array('app_id'    => "app's id",
        'usage'     => array('hits' => 1),
        'timestamp' => mktime(12, 36, 0, 4, 28, 2010))));

时间戳可以是Unix时间戳(整数)或字符串。字符串必须是strtotime函数可解析的格式。例如

"2010-04-28 12:38:33 +0200"

如果时间戳不是UTC,您必须指定一个时间偏移量。在上面的例子中,那就是"+0200"(比世界协调时间快两小时)。

然后调用返回响应对象的isSuccess()方法以查看报告是否成功

if ($response->isSuccess()) {
  // All OK.
} else {
  // There was an error.
}

在发生错误的情况下,getErrorCode()返回系统错误代码和getErrorMessage()可读的错误描述

$response->getErrorCode()    // "provider_key_invalid"
$response->getErrorMessage() // "provider key \"foo\" is invalid"

插件集成

如果您有兴趣将插件与以下内容集成

法律

版权所有 (c) 2010 3scale networks S.L.,许可协议为 MIT 协议。