Active Campaign PHP API Example

Paul Hill

October 25, 2020

Generally at Server Academy we provide training on Windows Server for IT professionals but this time we are writing about using PHP (a server side web programming language) to interact with the Active Campaign (email marketing) API key.

We were recently writing PHP code to create new email contacts with Active Campaign and came up with this little function:

// Active Campaign API call
function ActiveCampaignAPICall($Endpoint, $Method = "get", $PostBody = "", $ReturnTransfer = true) {
    
    // Variables
    $EndpointRoot = "https://youraccountname.api-us1.com";
    
    // Create event in Active Campaign
    $curl = curl_init();
    
    // Determine the method
    switch(strtolower($Method)) {
        case "get":
            curl_setopt($curl, CURLOPT_HTTPGET, true);
            break;
        case "post":
            curl_setopt($curl, CURLOPT_POST, true);
            break;
        case "put":
            curl_setopt($curl, CURLOPT_PUT, true);
            break;
        case "delete":
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
            break;
    }

    // Set the post body
    if($PostBody) {
        curl_setopt($curl, CURLOPT_POSTFIELDS, $PostBody);        
    }
    
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Api-Token: yourapitokengoeshere'));
    curl_setopt($curl, CURLOPT_URL, $EndpointRoot . $Endpoint);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, $ReturnTransfer);
    
    // Execute the curl request
    $result = curl_exec($curl);

    return $result;
}

This PHP function can be used like this to create a new contact in Active Campaign:

Get an existing contact

$contact = ActiveCampaignAPICall("/api/3/contacts/1234");

Create a new contact

$json = '{
	"contact": {
		"email": "johndoe@example.com",
		"firstName": "John",
		"lastName": "Doe",
		"phone": "7223224241"
	}
}';

$result = ActiveCampaignAPICall("/api/3/contacts", "post", $json);

Update an existing contact


$json = '{
	"contact": {
		"email": "johndoe@example.com",
		"firstName": "John",
		"lastName": "Friday"
,
		"phone": "12312341234"
	}
}';

$result = ActiveCampaignAPICall("/api/3/contact/sync", "post", $json);

Delete an existing contact

$result = ActiveCampaignAPICall("/api/3/contacts/17739", "delete");

I’m sure this function can be improved as I am no pro PHP developer…but hopefully some of you found it and the examples useful.

Blogpostctadesktop

Sign up free and start learning today!

Practice on REAL servers, learn from our video lessons, interact with the Server Academy community!

More from our blog

How to Become a Hacker

By Paul Hill | October 15, 2022

Becoming a hacker can be an exciting and rewarding six-figure job. Any product that uses digital technology is a potential target to be hacked – that means not just computers and phones but also your car, thermostats, garage door openers, smart coffee machines, and any other smart home device. That is why ethical/white hat hackers…

System Administrator Salary in 2022

By Ricardo P | August 27, 2022

Before deciding to become a System Administrator full-time, you might ask yourself, what is the system administrator salary and what can you expect? And, it is reasonable to research if becoming a System Administrator full-time will be a lucrative career. But the answer is that it all depends on your education, certifications, skills, location, and…

How to Easily Automate Tasks with PowerShell

By Ricardo P | July 1, 2022

You might know PowerShell as the improved command prompt version of Microsoft Windows, but PowerShell is more than that. It is also a scripting language that can be used for automation, and in this article we’ll show you how to automate tasks with PowerShell.

Assign License to a User using Licensing Group

By Jefferd Facundo | March 13, 2022

In this lesson, we will go through the steps on how to add user(s) to a licensing group in your Microsoft 365 tenant. Step 1: Access Microsoft Admin Center to manage the user’s license. On your browser, login to Microsoft 365 Admin Portal via https://portal.office.com On the Sign in page, enter your Global Admin username in an email…

Running Message Trace in Exchange Admin Center

By Jefferd Facundo | February 5, 2022

In this tutorial, you will learn how to run a message trace using Exchange Admin Center to track any messages that was not delivered correctly or simply getting the status of the message sent or supposed to be received by the user(s). Step 1. Log in to Exchange Admin Center for Exchange Online. On your…

How to Create a Licensing Group in Microsoft 365 Tenant

By Jefferd Facundo | February 4, 2022

In this lesson, we will go through the steps on how to Create a Licensing Group in your Microsoft 365 tenant. Step 1: Access Azure Active Directory On your browser, login to Microsoft 365 Admin Portal via https://portal.office.com On the Sign in page, enter your Global Admin username in an email format and then enter your password on…

Windows Cyber Security – STIGs for Beginners

By Paul Hill | February 1, 2022

This tutorial will show you how you can get started learning the technical side of Cyber Security for Windows environments. This tutorial is going to show you how to use STIGs (Security Technical Implementation Guides) to identify low, medium and high vulnerabilities and patch them in your Windows Environments. What is a STIG? A STIG…

Creating Mail Flow Rules

By Jefferd Facundo | January 19, 2022

In the lesson you will access the Exchange Admin Center for Exchange Online and create a mail flow rule that checks for sensitive information in emails sent from inside your organization. Step 1. Log in to Exchange Admin Center for Exchange Online. On your browser, login to Microsoft 365 Admin Portal via https://admin.microsoft.com On the…

How to run SQL Commands from PowerShell

By Paul Hill | July 23, 2021

If you want to run SQL commands from your PowerShell terminal you can do so by simply installing the SQLServer module with the command below: You will be prompted to install from an untrusted source. Type “A” to accept and install the module. Once it is complete you can see all the new commands you…

Adding Multiple Cloud Users to Microsoft 365 tenant.

By Jefferd Facundo | July 18, 2021

In this lesson, you will learn how to import multiple cloud users to your Microsoft 365 tenant. If you want to learn about getting started with Microsoft 365, click here. Step 1. Create your users’ information into CSV file using the given template in your tenant. On your browser, login to Microsoft 365 Admin Portal…