Zum Inhalt springen
← Zurück zu den Projekten

Laravel Activecampaign

Laravel ActiveCampaign

#Laravel ActiveCampaign

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads License

A PHP/Laravel client for the ActiveCampaign REST API v3. Covers contacts, lists, campaigns, deals (CRM), automations, tags, pipelines, webhooks and users through a simple, typed API built on Laravel's Http client.

#Features

  • Contacts: list, get, create, update, delete, sync (upsert by email)
  • Lists: list, get, create, delete, subscribe/unsubscribe a contact
  • Campaigns: list, get
  • Deals (CRM): list, get, create, update, delete
  • Automations: list, get, add a contact to an automation
  • Tags: list, get, create, delete, add/remove on a contact
  • Pipelines: list, get
  • Webhooks: list, get, create, delete
  • Users: current user (me), list
  • Throws ActiveCampaignException (with the original API error body) on any non-2xx response
  • Throws InvalidArgumentException before hitting the API when a required field is missing

#Installation

You can install the package via composer:

composer require jeffersongoncalves/laravel-activecampaign

Publish the config file:

php artisan vendor:publish --tag=activecampaign-config

Set your ActiveCampaign credentials in .env:

ACTIVECAMPAIGN_API_URL=https://youraccountname.api-us1.com
ACTIVECAMPAIGN_API_KEY=your-api-key

Both values are found under Settings > Developer in your ActiveCampaign account.

#Configuration

// config/activecampaign.php
return [
    'api_url' => env('ACTIVECAMPAIGN_API_URL', ''),
    'api_key' => env('ACTIVECAMPAIGN_API_KEY', ''),
    'default_limit' => env('ACTIVECAMPAIGN_DEFAULT_LIMIT', 20),
];

#Usage

The package is resolved via the ActiveCampaign facade or by injecting JeffersonGoncalves\ActiveCampaign\ActiveCampaign. Each resource is exposed as a method returning a dedicated resource class.

#Contacts

use JeffersonGoncalves\ActiveCampaign\Facades\ActiveCampaign;

// List (supports email, search, listid, status, limit, offset filters)
$contacts = ActiveCampaign::contacts()->list(['email' => 'jane@example.com']);

$contact = ActiveCampaign::contacts()->get(1);

$contact = ActiveCampaign::contacts()->create([
    'email' => 'jane@example.com',
    'firstName' => 'Jane',
    'lastName' => 'Doe',
]);

ActiveCampaign::contacts()->update(1, ['firstName' => 'Janet']);

ActiveCampaign::contacts()->delete(1);

// Upsert by email
ActiveCampaign::contacts()->sync(['email' => 'jane@example.com', 'firstName' => 'Jane']);

#Lists

$lists = ActiveCampaign::lists()->list();

$list = ActiveCampaign::lists()->create(['name' => 'Newsletter']);

ActiveCampaign::lists()->subscribe(listId: 1, contactId: 42);
ActiveCampaign::lists()->unsubscribe(listId: 1, contactId: 42);

ActiveCampaign::lists()->delete(1);

#Deals (CRM)

$deals = ActiveCampaign::deals()->list(['search' => 'Big Sale', 'stage' => 3]);

$deal = ActiveCampaign::deals()->create([
    'title' => 'Big Sale',
    'value' => 10000, // in cents
    'currency' => 'usd',
    'group' => 1, // pipeline id
    'stage' => 3,
    'contact' => 42,
]);

ActiveCampaign::deals()->update($deal['deal']['id'], ['stage' => 5]);

ActiveCampaign::deals()->delete($deal['deal']['id']);

#Tags

$tags = ActiveCampaign::tags()->list(['search' => 'vip']);

$tag = ActiveCampaign::tags()->create('vip');

ActiveCampaign::tags()->addToContact(tagId: $tag['tag']['id'], contactId: 42);
ActiveCampaign::tags()->removeFromContact($contactTagId);

ActiveCampaign::tags()->delete($tag['tag']['id']);

#Campaigns, Automations, Pipelines, Webhooks and Users

ActiveCampaign::campaigns()->list();
ActiveCampaign::campaigns()->get(1);

ActiveCampaign::automations()->list();
ActiveCampaign::automations()->addContact(automationId: 1, contactId: 42);

ActiveCampaign::pipelines()->list();

ActiveCampaign::webhooks()->create(
    name: 'CRM Sync',
    url: 'https://example.com/hooks/activecampaign',
    events: ['subscribe', 'unsubscribe'],
);

ActiveCampaign::users()->me();
ActiveCampaign::users()->list();

#Error handling

Any non-2xx API response throws JeffersonGoncalves\ActiveCampaign\Exceptions\ActiveCampaignException, which exposes the decoded error body:

use JeffersonGoncalves\ActiveCampaign\Exceptions\ActiveCampaignException;

try {
    ActiveCampaign::contacts()->get(999999);
} catch (ActiveCampaignException $e) {
    logger()->error($e->getMessage(), $e->errorBody());
}

Missing required fields (e.g. email on contacts()->create(), name on lists()->create()) throw InvalidArgumentException before any HTTP call is made.

#Testing

composer test

#Changelog

Please see CHANGELOG for more information on what has changed recently.

#Contributing

Please see CONTRIBUTING for details.

#Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

#Credits

#License

The MIT License (MIT). Please see License File for more information.

Neue Version verfügbar.