定制 ilrwebservices/campaign-monitor-rest-api-client 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ilrwebservices/campaign-monitor-rest-api-client

Composer 安装命令:

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

包简介

PHP library for the CampaignMonitor 'createsend' API

README 文档

README

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

CampaignMonitorRestClient is an extended GuzzleHttp\Client, so it can do all the things that Guzzle can and a bit more.

Be sure to refer to the Campaign Monitor API documentation for details.

Installation

Via composer:

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

Usage

<?php

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

use CampaignMonitor\CampaignMonitorRestClient;

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

Note how the API key is passed into the client constructor options along with other Guzzle-compatible options. In this example, the API key is stored in an environment variable since it is sensitive information that should not be stored in code.

The CampaignMonitorRestClient will set the base_uri option to https://api.createsend.com/api/v3.2/ automatically. To use a different base uri (for example for a different API version), you can specify it explicitly:

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

You can now make API calls just as you would in Guzzle:

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

Since the base_uri option is pre-configured, you can use a relative URL (be sure not to add a leading /, though):

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

And because it's Guzzle, there are shorthand methods:

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

Responses are PSR-7 response messages, just like Guzzle, so you can get returned data via getBody():

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

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

For application/json responses, CampaignMonitorRestClient adds a non-standard getData() method that decodes the JSON response for you:

$client_data = $client_response->getData();

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

Be careful with large responses, however, as they can consume too much memory.

Sending JSON data to API endpoints can be done using the json request option from Guzzle:

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

Error handling

You can use Guzzle exceptions for error handling. Campaign Montior endpoints generally use 4xx codes for errors, so GuzzleHttp\Exception\ClientException is a good match for catching those errors.

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());
}

If the above request to create a new client failed, a GuzzleHttp\Exception\ClientException would be thrown and the following output displayed:

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."}

Inspired by drewm/mailchimp-api.

ilrwebservices/campaign-monitor-rest-api-client 适用场景与选型建议

ilrwebservices/campaign-monitor-rest-api-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 30.3k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 09 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「mail」 「campaignmonitor」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 ilrwebservices/campaign-monitor-rest-api-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ilrwebservices/campaign-monitor-rest-api-client 我们能提供哪些服务?
定制开发 / 二次开发

基于 ilrwebservices/campaign-monitor-rest-api-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 30.3k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 31
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 5
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-09-24