Subscription Form integration using the API

Please note: there is a newer update available for this topic.

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

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

A unique idea might be to use our email marketing 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:

[sourcecode language='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);
[/sourcecode]

This will go out and obtain the information for form ID 1001, and set it to an array which we can reference easily.

HTML

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

[sourcecode language='php']
echo $result["html"];
[/sourcecode]

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:

[sourcecode language='php']
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"];
}
}
[/sourcecode]

Here’s how you’d call this function:

[sourcecode language='php']
$form_html = ac_api_form_view_html(1001);

echo $form_html;
[/sourcecode]

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 data each time the page is loaded. It might be wise to cache the subscription form HTML locally, then only re-fetch the updated data at a set interval, such as weekly.

Our WordPress plugin performs the same type of caching, if desired.

Let us know if you have any questions or suggestions for the API.

A trial is worth a thousand words.
Get started today, no credit card required.

 
 
  • Simon

    I have a form already on my worpress site and I want to know how I can use the API to enable people to be subscribed to a specified list when they submit the form?

  • Hi Simon, you’d have to use PHP to interact with the API. Specifically, the “subscriber_add” action. You can find an example of that in the docs/api-examples folder of your Email Marketing software. Please let me know if you have any questions.

  • Simon

    Hi Matt,

    Thank you for your reply, is it possible to be more specific, I’ve been looking at the api examples and although I have an idea of what to do with the examples and I can run the example directly from a browser to get it to add a subscriber, I do not know how to include that in the form I use, are there any instructions on how to do this? or what should be done?

    Thanks again

  • Hi Simon, just like the PHP example file creates a post array, and submits via curl, you’d have to do the same in your code.

    Please feel free to email me directly (mthommes AT activecampaign DOT com) if you need further assistance. Thanks.