Active Campaign PHP API Example

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 = "";
    // Create event in Active Campaign
    $curl = curl_init();
    // Determine the method
    switch(strtolower($Method)) {
        case "get":
            curl_setopt($curl, CURLOPT_HTTPGET, true);
        case "post":
            curl_setopt($curl, CURLOPT_POST, true);
        case "put":
            curl_setopt($curl, CURLOPT_PUT, true);
        case "delete":
            curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");

    // 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": "",
		"firstName": "John",
		"lastName": "Doe",
		"phone": "7223224241"

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

Update an existing contact

$json = '{
	"contact": {
		"email": "",
		"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.

Try one of our IT labs

Want to try one of our online IT labs? Enter your email below and we will send you an Active Directory IT lab!

Table of Contents
    Add a header to begin generating the table of contents

    Free trial to boost your IT skills

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