adobe-marketing-cloud/marketing-cloud-php-sdk

Adobe Marketing Cloud API 的面向对象的包装器

2.1.2 2018-03-27 19:03 UTC

This package is auto-updated.

Last update: 2024-08-29 04:03:21 UTC


README

Build Status

这是一个简单的、基于 PHP5 编写的面向对象的 Adobe Marketing Cloud API 包装器。此库仿照了由 ornicar 创建的 php-github-api

使用 Adobe Marketing Cloud API。默认版本为 1.4,但 1.3 和 1.2 也兼容。

要求

  • PHP 5.3 或更高版本
  • php curl,但也可以编写另一个传输层。

如果你需要的函数尚不存在,请不要犹豫,通过 问题 提出它!

自动加载

使用 composer 包含库的第一步

require_once '/vendor/autoload.php';

实例化一个新的 AdobeMarketingCloud 客户端

$adm = new AdobeMarketingCloud\Client();

从这个对象中,您可以现在访问所有不同的 AdobeMarketingCloud API(如下所示)

用户认证

使用您的 Adobe 数字营销 Web 服务用户名和密码进行认证。您可以通过登录到 Marketing Cloud 并浏览到 Admin > Company > Web Services 来获取一个。

$adm->authenticate($username, $secret);

注意:如果认证失败,可能是因为您的公司端点与默认端点不同。为了验证这一点,请调用 getEndpoint() 方法

echo $adm->getCompanyApi()->getEndpoint('My Company Name');
// "https://api2.omniture.com"

如果此函数的返回值与 https:api.omniture.com 不同,您需要在客户端设置端点

$adm->setEndpoint('https://api2.omniture.com');

用户注销

取消认证。

$adm->deAuthenticate();

之后的请求将不再进行认证

报告

用于排队 SiteCatalyst 报告的包装器 SiteCatalyst 报告 API

$reportApi = $adm->getReportApi();

运行排名报告

$response = $reportApi->queueReport(array(
    'reportSuiteID' => 'your-id',
    'date'     => date('Y-m-d'),
    'metrics'  => array(
        array('id' => 'pageviews'),
    ),
    'elements' => array(
        array('id' => 'eVar1'),
    ),
));

print_r($response);

上面的代码将渲染队列报告的状态,看起来可能像这样

Array
(
  [status]    => ready
  [statusMsg] => Your report has been queued
  [reportID] => 123456789
)

检索队列报告

一旦检索到趋势、排名或超时报告的报告 ID,请使用 Report.GetReport API 调用来检索报告

$response = $reportApi->queueReport(array(
    //... (see above)
));

$reportId = $response['reportID'];

do {
    $report = $reportApi->getReport($reportId);
    sleep(2);
} while ($report['status'] == 'not ready');

print_r($report['report']);

上面的代码将渲染 Report 数组,看起来可能像这样

Array
(
    [reportSuite] => Array (
        [id]      => your-id
        [name]    => Your Report Suite
    )
    [period]      => "Mon. 16 July 2012",
    [elements]    => Array (
        [id]      => eVar2,
        [name]    => eVar 2
    )
    [metrics]     => Array (
        [id]      => event2
        [name]    => Page View Event
        [type]    => number
    )
    [type]        => trended
    [data]        => Array (
        ...
    )
    [totals]      => Array (
        ...
    )
)

返回如 文档 所述的结果数组

调用其他方法

可以通过 SuiteApi 类调用 API 探索器中可见的所有方法。这是一个接受方法名称和参数的通用类,并将返回结果的 json 编码响应。

$adm->getSuiteApi()->post('Saint.ExportCreateJob', $parameters);

Curl 调试

如果请求返回 null,请在客户端上调用 getLastResponse 方法,以查看 curl 信息和原始响应

if (!$response = $adm->getReportApi()->getReport($reportId)) {
  print_r($adm->getLastResponse());
}

将调试标志传递给 HttpClient 将在响应不存在或不是 json 格式时自动输出响应

$adm = new AdobeMarketingCloud\Client(
  new AdobeMarketingCloud\HttpClient\Curl(array('debug' => true))
);

命令行实用程序

OAuth 尚不可用于生产使用。此功能仍在开发中。

开始使用OAuth最简单的方法是使用命令行实用工具(bin/adm)。

要开始,请复制配置文件

$ cp config/profile.json.dist config/profile.json

完成此操作后,运行adm命令以开始

$ ./bin/amc

Calls the Adobe Marketing Cloud APIs
To get started, call

    $ amc authorize

to retrieve a token.  Some other options available are

 -h, --help      Display a help message and exit
 -v, --version   Display the current api version
 -e, --endpoint  Specify the api endpoint

See developer.omniture.com for more information

第一步是获取一个OAuth令牌。这可以通过向authorize命令提供客户端ID、客户端密钥、用户名和密码来实现。可以在开发者连接中创建客户端ID。完成后,运行authorize命令以接收令牌

$ adm authorize CLIENT_ID CLIENT_SECRET USERNAME PASSWORD
Token: {
  "access_token":"f9f071ca2c25d23eff01a1ea238d6f85666be0f6",
  "expires_in":2592000,
  "token_type":"bearer",
  "scope":null,
  "success":true
}

您现在已收到第一个访问令牌。这已被保存到config/profile.json中,因此您无需担心。您可以开始发送请求了!

$ adm request Company.GetReportSuites
Array
(
   [report_suites] => Array
       (
           [0] => Array
               (
                   [rsid] => your.rsid
                   [site_title] => Your Site
               )
           ...
       )
)

端点

默认端点是api.omniture.com。如果您遇到问题,请调用Company.GetEndpoint方法以找出您应该使用哪个端点

$ ./bin/adm Company.GetEndpoint 'company=My Company'
https://api2.omniture.com/admin/1.3/rest/

在这种情况下,端点是api2.omniture.com。要使用此端点,请在您的配置文件中设置端点

$ ./bin/amc profile endpoint api2.omniture.com
default value for "endpoint" set to api2.omniture.com

现在所有后续请求都将使用此端点。如果您需要使用除默认端点之外的其他端点,可以使用-e--endpoint选项传递此端点

$ ./bin/amc authorize CLIENT_ID CLIENT_SECRET USERNAME PASSWORD --endpoint 'api3.omniture.com'

待办事项

更好的文档和测试覆盖率将很快到来