aivakha/activecampaign-wrapper-v3

ActiveCampaign API v3 包装器

1.0.1 2022-01-20 12:01 UTC

This package is auto-updated.

Last update: 2024-09-20 17:58:26 UTC


README

ActiveCampaign API v3 包装器。
允许使用 PHP 调用 ActiveCampaign 服务并处理数据。简单易用。
目前可用的服务

  • 列表
  • 联系人
  • 标签

官方 API 文档

https://developers.activecampaign.com/v3/reference

安装

ActiveCampaign API v3 包装器 可在 Packagist 上找到。只需将此行添加到您的 composer.json 文件中的 require 部分

"aivakha/activecampaign-api-v3-wrapper": "^1.0"

或者打开终端窗口并运行

composer require aivakha/activecampaign-api-v3-wrapper

使用方法

包装器允许您链式调用方法,并使用单个实例应用所有必要的过滤器和查询。

设置

<?php

// include required classes
require 'vendor/autoload.php';

// add namespace
use ActiveCampaign\ActiveCampaign;

// create wrapper instance
$ac = new ActiveCampaign('YOUR_ACTIVE_CAMPAIGN_URL', 'YOUR_ACTIVE_CAMPAIGN_KEY');

获取服务模型

要访问 activecampaign API 端点,您需要首先创建服务模型。以下是如何做到这一点的示例

// lists
$lists = $ac->lists();

// contacts
$contacts = $ac->contacts();

// tags
$tags = $ac->tags();

基本示例

要检索所有列表,请调用 ->all() 方法。此方法应始终位于您的链式序列的末尾

$lists = $ac->lists()->all();

请注意,默认情况下,activecampaign API 返回 20 项。要更改此设置,您需要使用 pagination() 方法

分页

https://developers.activecampaign.com/v3/reference#pagination
分页允许您获取所需数量的项并设置偏移量。

// ->paginate($limit, $offset = 0)

// fetch 50 lists
$paginated_lists = $ac->lists()->paginate(50)->all();

排序

https://developers.activecampaign.com/v3/reference#section-ordering
您可以根据所需顺序排序结果。使用 ->orderby() 方法,并传递一个数组作为参数,其中键是字段名称,值是顺序(升序或降序)。

// get all contacts and sort them by email in asc order and by last name in desc order
$contacts = $ac->contacts()->order(['email' => 'asc', 'lastName' => 'desc'])->all();

过滤

https://developers.activecampaign.com/v3/reference#section-filtering
您可以根据多个参数过滤结果。使用 ->filter() 方法,并传递一个数组作为参数,其中键是参数名称,值是参数值。

// get contacts where first name is equal to John
$contacts = $ac->contacts()->filter(['firstName' => 'john'])->all();

URL 查询

此外,您可以将任何参数添加到将发送到 activecampaign 端点的 URL。使用 ->query() 方法,并传递一个参数键和值的数组作为参数

$ac->tags()->query(['foo' => 'bar'])->all();

通过 ID 获取项目

要访问任何项目及其 ID,请使用 ->get($id) 方法。

// get tag with ID == 1
$tag = $ac->tags()->get(1);

高级示例

// skip 10 tags and get next 50 tags, also order them by description
$tags = $ac->tags()->orderby(['description' => 'asc'])->paginate(50, 10)->all();

// get contact where email is equal to 'john@mail.com'
$contact = $ac->contacts()->getByEmail('john@mail.com');

// create new contact
$ac->contacts()->create([
  'email'     => 'johndoe@example.com',
  'firstName' => 'John',
  'lastName'  => 'Doe',
  'phone'     => '7223224241'
]);

// create new tag
$ac->tags()->create([
  'tag'         => 'My Tag',
  'tagType'     => 'contact',
  'description' => 'Description'
]);

// add tag to contact
$ac->contacts()->addTag([
  'contact' => '1', // contact ID
  'tag'     => '20' // tag ID
]);

可用方法

列表

https://developers.activecampaign.com/v3/reference#lists

注意:创建新列表后,非常重要地将该列表关联到组权限

联系人

https://developers.activecampaign.com/v3/reference#contact

标签

https://developers.activecampaign.com/reference#tags