swco/appnexusapi

访问AppNexus只读API的接口

v1.4.2 2015-10-27 08:50 UTC

README

Latest Stable Version Build Status License

安装

此库需要PHP 5.3或更高版本,可以通过Composer安装和自动加载,名称为swco/appnexusapi

入门

以下示例展示了您可以通过几种不同的方式访问数据。

当通过辅助函数的get*方法访问数据时,请求会立即发送并返回一个对象数组。

当使用get($service, $reset = true)方法时,您可以在调用send()之前继续应用过滤条件。使用send()时,您也会得到一个Response对象,而不是一个常规数组。这为您提供了访问更多方法的机会,如getStatus()getStartElement()

get()方法传递的第二个参数允许您重置所有过滤器(默认操作)。传递false将阻止调用reset()

use \SWCO\AppNexusAPI\Request;

$request = new Request("username", "password");

// Get category ID 1 and 7
$categories = $request->whereId(array(1, 7))->getCategories();

// Get category ID 5
$category = $request->getCategory(5);

// Get all brands update since June 2014
$brands = $request->get(Request::SERVICE_BRAND)->since(new DateTime('June 2014'))->send();
echo $brands->getStatus();// OK

// Domain Audit Statuses are a bit different as they needs some post data
$domainAuditStatus = $request->getDomainAuditStatuses(array('google.com'));

所有服务的参数默认为它们的假值;

string = '';
int = 0;
bool = false;
array = array();
float = 0.0;

此规则的唯一例外是应该为对象的参数,如果未设置,则返回null;

object = null;

高级

一些服务(目前是Brand)有自己的特殊过滤器,这伴随着一个请求包装器;

use \SWCO\AppNexusAPI\Request;
use \SWCO\AppNexusAPI\BrandRequest;

$request = new Request("username", "password");

$brand = BrandRequest::newFromRequest($request);

$brand->simple();

...

这允许提供额外的功能,例如访问上述的simple()方法,该方法删除了num_creatives数据,使API调用变得更快。

您还可以通过辅助函数getBrand()getBrands()发送简单的标志;

use \SWCO\AppNexusAPI\Request;
use \SWCO\AppNexusAPI\BrandRequest;

$request = new Request("username", "password");

$brand = BrandRequest::newFromRequest($request);

$simple = true;
$brand->getBrand(1, $simple);

身份验证

对于更重的使用,值得存储令牌以供重用。令牌不会更改,库在需要时处理重新身份验证。

*令牌偶尔会更改。如果配置中存储了旧令牌,请求将重新身份验证以获取正确的令牌。目前没有报告此问题的方法。

身份验证可以通过请求对象或直接使用Auth对象来处理;

use \SWCO\AppNexusAPI\Request;

$request = new Request("username", "password");
$token = $request->auth();

// Store $token somewhere
use \SWCO\AppNexusAPI\Auth;
use \Guzzle\Http\Client;
use \SWCO\AppNexusAPI\Request;
use \SWCO\AppNexusAPI\Exceptions\NoAuthException;

$auth = new Auth();
$auth->setClient(new Client(Request::APP_NEXUS_API_URL));
try {
    $token = $auth->auth('username', 'password');
} catch (NoAuthException $e) {
    $token = null;
}

if ($token) {
    // Store $token somewhere
}

数据池

通常,API对每个请求的限制是100个结果。有一个DataPool对象可用于通过多次请求获取所有或特定数量的结果;

use SWCO\AppNexusAPI\DataPool;
use SWCO\AppNexusAPI\Request;

$request = new Request('username', 'password');
$request->get(Request::SERVICE_DEVICE_MAKE);

$dataPool = new DataPool();

$data = $dataPool->getAll($request); // Gets all Device Make items

$data = $dataPool->get($request, 250); // Gets the first 250 results of the Device Make items.

$request->offsetBy(250);

$data = $dataPool->get($request, 250); // Gets items 250 - 500.