mailoptin/mailchimp-api-php

PHP 库,用于 MailChimp API 的第 3 版

2.1.3 2023-03-05 10:49 UTC

README

此库为 Mailchimp 的 REST API 提供方便的包装函数。API 的文档在这里

要求

  • PHP 5.4.0 或更高版本(如果您想使用 phpunit,则需 7.0 或更高版本)
  • Composer
  • Guzzle

安装

依赖关系由 Composer 管理。安装 Composer 后,从库根目录运行以下命令:

composer install --no-dev --ignore-platform-reqs

或者要使用 phpunit 安装

composer install

用法

获取您的账户信息

一个基本的测试,以确认库已设置并运行。

<?php
require 'PATH_TO_LIBRARY/mailchimp-api-php/vendor/autoload.php';
$api_key = 'YOUR_API_KEY';
$mailchimp = new Mailchimp\Mailchimp($api_key);

// Get the account details of the authenticated user.
$response = $mailchimp->getAccount();

// Output the account details.
if (!empty($response) && isset($response->account_id)) {
  echo "ID: {$response->account_id}\n"
  . "Name: {$response->account_name}\n";
}

获取列表及其兴趣类别

一个更复杂的例子,它使用一个 API 调用的响应并使用这些数据来执行另一个。

<?php
require 'PATH_TO_LIBRARY/mailchimp-api-php/vendor/autoload.php';
$api_key = 'YOUR_API_KEY';
$mailchimp_lists = new Mailchimp\MailchimpLists($api_key);

// Get all lists.
$response = $mailchimp_lists->getLists();

if (!empty($response) && isset($response->lists)) {
  foreach ($response->lists as $list) {
    // Output the list name.
    echo "List name: {$list->name}\n";

    // Get the list's interest categories.
    $interests = $mailchimp_lists->getInterestCategories($list->id);

    // Output the names of the list's interest categories.
    if (!empty($interests) && isset($interests->categories)) {
      foreach ($interests->categories as $category) {
        echo "Interest category: {$category->title}\n";
      }
    }
  }
}

测试

此库包括一个 PHPUnit 测试套件。

运行 PHPUnit 测试

将 Composer 的 vendor 目录添加到您的 PATH 中,通过在您的配置文件中添加以下行。这取决于您的系统,但在 Linux 或 Mac OSX 系统中使用 Bash,您通常可以在 ~/.bash_profile 文件中找到它。

export PATH="./vendor/bin:$PATH"

Bash 示例

echo 'export PATH="./vendor/bin:$PATH"' >> ~/.bash_profile
source ~/.bash_profile

然后运行 PHPUnit

phpunit

Mailchimp API 演示场

Mailchimp 的 API 演示场 通过基于 Web 的 UI 提供对所有 API 方法的访问。您可以使用它来测试 API 调用并审查发送到 Mailchimp 的数据。