Help articles

 

Exporting Contact Data Using Our API

There might be times when you need to export your contact data from outside of our application. Maybe you want to backup your data, or you need to analyze every contact in your account.

Within our interface we provide a quick “Export” option that produces a CSV or XML file which can be easily imported to a spreadsheet program, or read through a programming language.

You can also create a webhook that will automatically send data to your application whenever a new contact is added or updated.

Despite these two features, there still might be times when your application needs to access the data on-demand. We’ll show you how to do this using PHP.

First, set up your API connection from your script:

define("ACTIVECAMPAIGN_URL", "https://ACCOUNT.api-us1.com");
define("ACTIVECAMPAIGN_API_KEY", "0c3b245df4785...005d2d3d7e1e386f");
require_once("activecampaign-api-php/includes/ActiveCampaign.class.php");
$ac = new ActiveCampaign(ACTIVECAMPAIGN_URL, ACTIVECAMPAIGN_API_KEY);

Next, we’ll create a function that handles the API request and adds each contact to a global array. The function accepts a page number, as well as optional list ID’s (if you want to request contacts only from certain lists), and returns the total number of contacts found for the page requested.

$GLOBALS["contacts"] = array(); // Holds all contacts.

function run($page, $listids = 0) {

  // API method.
  $request = "contact/list";

  // Check for list filter. If none, request all contacts.
  if ($listids)
    $request .= "?filters[listid]={$listids}";
  else
    $request .= "?ids=all";

  // Make request.
  $request .= "&full=0&sort=id&sort_direction=ASC&per_page=500&page={$page}";
  $response = $GLOBALS["ac"]->api($request);

  // Internal counter.
  $count = 0;

  // If request comes back successfully.
  if ((int)$response->success) {

    // Loop through each item.
    foreach ($response as $key => $value) {

      // If there is an email item, we know it's a contact.
      if (isset($value->email)) {

        $count++; // Increment the internal counter.

        // Store this contact in the global array.
        if (!isset($GLOBALS["contacts"][$value->id])) {
          $GLOBALS["contacts"][$value->id] = $value;
        }

      }

    }

  }
  else {

    // Request failed. Output the reason.
    echo $response->error;

  }

  return $count;

}

As you can see above (highlighted), we add each contact to a global array ($contacts). Here is a sample contact record:

stdClass Object
(
  [id] => 29564
  [subscriberid] => 29564
  [cdate] => 2012-12-14 09:01:00
  [sdate] => 2012-12-14 09:01:00
  [first_name] => Mike
  [last_name] => Smith
  [email] => test@test.com
  [last_list] => Rewards List
  [avatar_url] => https://test.api-us1.com/gravatar.php?h=test@test.com&s=50
)

You’ll also see above that we limit the results to 100 per page. This is the maximum amount you can receive for each page. To request subsequent pages, we’ll add some more code:

$page_start = 1; // The page to start from.
$page_end = 1; // The page to end on.
$page_loop = 0;
$listids = 9; // Any list filters (use comma-separated format for more than one: 1,2)

while (($page_start + $page_loop) <= $page_end) {
  $page_current = $page_start + $page_loop;
  $count = run($page_current, $listids);
  $page_loop++;
  sleep(3);
}

The loop above will automatically fetch the next page of contacts until it reaches the last page that you specify. You can easily find your last page by dividing the number of total contacts in your account with 100. For example, if you have ~1,000 contacts - you would need about 10 pages to fetch them all.

$page_start = 1; // The page to start from.
$page_end = 10; // The page to end on.
...

You could also change the loop to look only at the total number returned ($count):

while ($count == 100) ...

This way you are only fetching the next page if the current page has the maximum amount returned. If the current page has less than the maximum amount, you know it's the last page. We have found this approach a little less reliable and less flexible than our initial example, but it's up to you how you want to handle it.