PHP code example of xdubois / activecampaign-api

1. Go to this page and download the library: Download xdubois/activecampaign-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

xdubois / activecampaign-api example snippets


use xdubois\ActiveCampaign\ActiveCampaignAPI;

$api = new ActiveCampaignAPI('your-api-token', 'https://yourapp.api-us1.com');

use xdubois\ActiveCampaign\ActiveCampaignAPI;
use xdubois\ActiveCampaign\Configuration;

$config = new Configuration(
    'your-api-token',
    'https://yourapp.api-us1.com',
    timeout: 60,           // Request timeout in seconds
    connectTimeout: 10,    // Connection timeout in seconds
    maxRetries: 3,         // Number of retry attempts
    retryDelay: 1.0        // Delay between retries in seconds
);

$api = new ActiveCampaignAPI($config);

// Create a contact
$contact = $api->contacts()->create([
    'contact' => [
        'email' => '[email protected]',
        'firstName' => 'John',
        'lastName' => 'Doe',
        'phone' => '+1234567890'
    ]
]);

// Get a contact
$contact = $api->contacts()->get(123);

// Update a contact
$updatedContact = $api->contacts()->update(123, [
    'contact' => [
        'firstName' => 'Jane'
    ]
]);

// List contacts with filtering
$contacts = $api->contacts()->list([
    'limit' => 20,
    'offset' => 0,
    'search' => '[email protected]'
]);

// Sync a contact (create or update)
$syncedContact = $api->contacts()->sync([
    'contact' => [
        'email' => '[email protected]',
        'firstName' => 'John Updated'
    ]
]);

// Create a deal
$deal = $api->deals()->create([
    'deal' => [
        'title' => 'New Website Project',
        'value' => 500000, // Value in cents
        'currency' => 'USD',
        'group' => 1,      // Pipeline ID
        'stage' => 1,      // Stage ID
        'contact' => 123   // Contact ID
    ]
]);

// Add a note to a deal
$note = $api->deals()->createNote(456, [
    'note' => [
        'note' => 'Initial client meeting went well'
    ]
]);

use xdubois\ActiveCampaign\Exceptions\AuthenticationException;
use xdubois\ActiveCampaign\Exceptions\RateLimitException;
use xdubois\ActiveCampaign\Exceptions\ValidationException;
use xdubois\ActiveCampaign\Exceptions\NotFoundException;
use xdubois\ActiveCampaign\Exceptions\ActiveCampaignException;

try {
    $contact = $api->contacts()->create($data);
} catch (AuthenticationException $e) {
    // 401/403
    echo "Authentication failed: " . $e->getMessage();
} catch (ValidationException $e) {
    // 422
    echo "Validation failed: " . $e->getMessage();
    $errors = $e->getValidationErrors();
} catch (RateLimitException $e) {
    // 429
    echo "Rate limited. Retry after: " . $e->getRetryAfter() . " seconds";
} catch (NotFoundException $e) {
    // 404
    echo "Resource not found: " . $e->getMessage();
} catch (ActiveCampaignException $e) {
    // All other API errors
    echo "API error: " . $e->getMessage();
    $context = $e->getContext();
}