Integrate Subscription Forms using the API

Subscription Forms are excellent ways to promote your newsletter on any website, even Facebook.


It's often cumbersome to have to manually update every webpage that you have embedded subscription form HTML on, when any changes are made to the form.

A better idea might be to use Civil Dispatch API to display a subscription form. This tutorial will briefly walk through how to use PHP and our API to display subscription forms. Then, when changes are made to the subscription form, you no longer have to update every instance of the form across the web!

Note: Please be aware that doing this will cause repeated requests to the external domain, which can slow down sites, especially those with high traffic. This example, more or less, introduces how simple it is to use the API.

Obtaining your subscription form ID

First, visit your Subscription Form page in the admin section, and choose which form you want to display on an external website. Click the "View" link next to the subscription form name. Then note the unique number at the end of the URL (in this case, 1001):

Screenshot of ActiveCampaign email marketing software

This is the ID of the subscription form. We'll use this in our API call.

PHP

In PHP, set up your script to make an API call using the form_view action:

<?php

// Base API path
$path = "http://mysite.com/admin/api.php?";

// Initialize curl request
$request = curl_init($path . "api_user=admin&api_pass=test&api_action=form_view&api_output=serialize&id=1001&generate=1");

// Set other curl details
curl_setopt($request, CURLOPT_HEADER, 0);
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);

// Get the response
$response = (string)curl_exec($request);

// Close curl
curl_close($request);

// Set final result array to variable
$result = unserialize($response);

?>

This will go out and obtain the information for form ID 1001.

HTML

Now you just need to write out the HTML for your subscription form:

<?php

echo $result["html"];

?>

That's it! The entire HTML structure of the form can be obtained through a simple API call.

Improvements

If you have a few forms you want displayed on various spots around the web, it might be wise to write a function for yourself, so you don't have to repeat the curl code for each form you display:

function ac_api_form_view_html($id)
{
  $path = "http://mysite.com/admin/api.php?";

  $request = curl_init($path . "api_user=admin&api_pass=test&api_action=form_view&api_output=serialize&id=" . $id . "&generate=1");
  curl_setopt($request, CURLOPT_HEADER, 0);
  curl_setopt($request, CURLOPT_RETURNTRANSFER, 1);
  $response = (string)curl_exec($request);
  curl_close($request);
  $result = unserialize($response);

  if ($result["response_code"])
  {
    return $result["html"];
  }
}

Here's how you'd call this function:

$form_html = ac_api_form_view_html(1001);

echo $form_html;

Two lines is not bad!

Going further

As mentioned at the beginning, using the API in this manner might result in slower site performance, since your script has to re-request the subscription form each time the page is loaded. It might be wise to cache the subscription form HTML locally, then only re-fetch the updated subscription form at a set interval, such as weekly.

We also have a Wordpress plugin for subscription forms which performs the same type of caching, if desired.