ilrwebservices/campaign-monitor-rest-api-client

用于 CampaignMonitor 'createsend' API 的 PHP 库

1.0.1 2021-09-24 19:10 UTC

This package is auto-updated.

Last update: 2024-09-25 02:26:32 UTC


README

A simple PHP http client for the Campaign Monitor REST API. Optimized for JSON and authentication via API key. Based on Guzzle.

CampaignMonitorRestClient 是一个扩展的 GuzzleHttp\Client,因此它可以执行 Guzzle 可以执行的所有操作,并且还可以做更多。

请务必参考 Campaign Monitor API 文档 以获取详细信息。

安装

通过 composer

composer require ilrwebservices/campaign-monitor-rest-api-client

使用方法

<?php

require __DIR__ . '/../vendor/autoload.php';

use CampaignMonitor\CampaignMonitorRestClient;

$client = new CampaignMonitorRestClient([
  'api_key' => $_ENV['CAMPAIGN_MONITOR_API_KEY'],
]);

注意 API 密钥是如何通过客户端构造函数选项传递的,包括其他与 Guzzle 兼容的选项。在这个例子中,API 密钥存储在环境变量中,因为它是敏感信息,不应该存储在代码中。

CampaignMonitorRestClient 将自动将 base_uri 选项设置为 https://api.createsend.com/api/v3.2/。要使用不同的基本 URI(例如,用于不同的 API 版本),您可以明确指定它。

$client = new CampaignMonitorRestClient([
  'api_key' => $_ENV['CAMPAIGN_MONITOR_API_KEY'],
  'base_uri' => 'https://api.createsend.com/api/v3.1/',
]);

您现在可以像在 Guzzle 中一样进行 API 调用

$client_response = $client->request('GET', 'https://api.createsend.com/api/v3.2/clients.json');

由于 base_uri 选项已经预先配置,您可以使用相对 URL(但请确保不要添加前导 /

$client_response = $client->request('GET', 'clients.json');

由于它是 Guzzle,因此存在简写方法

$client_response = $client->get('clients.json');

响应是 PSR-7 响应消息,就像 Guzzle 一样,因此您可以通过 getBody() 获取返回的数据

$client_data_raw = (string) $client_response->getBody();

// Returns:
// [{"ClientID":"86753099713df60ae6545c9b338e3210","Name":"BungeeMan Enterprises"}]

对于 application/json 响应,CampaignMonitorRestClient 添加了一个非标准的 getData() 方法,它会为您解码 JSON 响应

$client_data = $client_response->getData();

// Returns:
// Array
// (
//     [0] => Array
//         (
//             [ClientID] => 86753099713df60ae6545c9b338e3210
//             [Name] => BungeeMan Enterprises
//         )
// )

但是,请注意大型响应,因为它们可能会消耗太多内存。

可以使用 Guzzle 的 json 请求选项将 JSON 数据发送到 API 端点

$new_client_response = $client->post('clients.json', [
  'json' => [
    'CompanyName': 'Cyberdyne Systems',
    'Country': "Australia",
    'TimeZone': "(GMT+10:00) Canberra, Melbourne, Sydney"
  ],
]);

错误处理

您可以使用 Guzzle 异常 进行错误处理。Campaign Monitor 端点通常使用 4xx 代码表示错误,因此 GuzzleHttp\Exception\ClientException 是捕获这些错误的良好选择。

try {
  $new_client_response = $client->post('clients.json', [
    'json' => [
      'CompanyName' => 'Cyberdyne Systems',
      'Country' => 'Australia',
      'TimeZone' => '(GMT+10:00) Canberra, Melbourne, Sydney',
    ],
  ]);
}
// Could not connect to server or other network issue.
catch (\GuzzleHttp\Exception\ConnectException $e) {
  print_r($e->getMessage());
}
// 4xx error. This is the bulk of Campaign Monitor API errors, and details about
// the error can be found in the error message and response data.
catch (\GuzzleHttp\Exception\ClientException $e) {
  print_r($e->getResponse()->getData());
  print_r($e->getMessage());
}
// 5xx error. This is an 'unhandled API error' in Campaign Monitor.
catch (\GuzzleHttp\Exception\ServerException $e) {
  print_r($e->getResponse()->getData());
  print_r($e->getMessage());
}

如果上述创建新客户端的请求失败,则会抛出 GuzzleHttp\Exception\ClientException 异常,并显示以下输出

Array
(
    [Code] => 51
    [Message] => Not allowed for a Non-agency Customer.
)
Client error: `POST https://api.createsend.com/api/v3.2/clients.json` resulted in a `403 Forbidden` response:
{"Code":51,"Message":"Not allowed for a Non-agency Customer."}

灵感来源于 drewm/mailchimp-api