treehousetim / activecampaign-api-v3-wrapper
PHP SDK ActiveCampaign API v3
Requires
README
ActiveCampaign API v3包装器。允许使用PHP调用ActiveCampaign服务并处理数据,简单易用。目前提供的服务
- 列表
- 联系人
- 标签
- 自定义字段
官方API文档
https://developers.activecampaign.com/v3/reference
安装
ActiveCampaign API v3 Wrapper可在Packagist上找到。只需将以下行添加到您的composer.json文件中的require部分
"treehousetim/activecampaign-api-v3-wrapper": "^1.0"
或者打开终端窗口并运行
composer require treehousetim/activecampaign-api-v3-wrapper
使用
包装器允许您链接方法并使用单个实例应用所有需要的过滤器和查询。
设置
<?php // include required classes require 'vendor/autoload.php'; use treehousetim\ActiveCampaign\ActiveCampaign; // using hard coded values $ac = new ActiveCampaign( 'ACTIVE_CAMPAIGN_URL', 'ACTIVE_CAMPAIGN_KEY'); // using environment variables $ac = new ActiveCampaign();
使用环境变量需要在.env文件中或在你的环境中添加条目
获取服务模型
要访问active campaign API端点,您需要先创建一个服务模型。
// lists $lists = $ac->lists(); // contacts $contacts = $ac->contacts(); // tags $tags = $ac->tags(); // custom fields $fields = $ac->customFields();
基本示例
要检索所有列表,调用->all()
方法。此方法应始终位于您的链式调用序列的末尾
$lists = $ac->lists()->all();
注意,Active Campaign API默认返回20个项目。
分页
https://developers.activecampaign.com/reference/pagination
分页允许您获取所需数量的项目并设置偏移量。注意API限制为最多100个项目。
// fetch the first 50 lists $limit = 50; $offset = 0; $paginated_lists = $ac->lists()->paginate( $limit, $offset )->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()->orderby( ['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查询
此外,您可以将任何参数添加到URL中,这些参数将被发送到activecampaign端点。使用->query()
方法并传递一个包含参数键和值的数组作为参数
$ac->tags()->query(['foo' => 'bar'])->all();
通过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 ]);