Using Long Custom Fields In Aweber’s API

So far I’ve loved the Aweber API and it’s usefulness in creating various small web apps for clients and for my own projects.

One issue that I quickly ran into however for Mantra Bot was trying to add a custom field that was fairly long. I wanted to add functionality for a mantra that was up to 300 characters long, but Aweber only accepts custom fields that are 100 characters long.

Luckily this can be broken up fairly easily with some easy PHP.

Here is the standard function for adding a subscriber to Aweber using the API:

$subscriber = array(
'email'  => $email,
'custom_fields' => array(
'field_name' => $mantra,
),
);
$app->addSubscriber($subscriber, $list);

But if we want to include a mantra that is longer then 100 characters, say 300 characters in my case, we need to split up the variable into 3 different custom fields.

We can do this rather easily using PHP’s str_split() function.

Here’s an example:

$mantrasplit = str_split($mantratext,100);

This line will take the input from the user ($mantratext) and split it up into an array every 100 characters.

If the input is 150 characters long you will end up with 2 values in your array ($mantrasplit). The first will be 100 characters long and the second will be 50.

Here’s our new Aweber function:

$subscriber = array(
'email'  => $email,
'custom_fields' => array(
'field_name1' => $mantrasplit[0],
'field_name2' => $mantrasplit[1],
'field_name3' => $mantrasplit[2],
),
);
$app->addSubscriber($subscriber, $list);

Then in Aweber you can display your long field by including all three custom fields as a merge field in a row with no spaces between them.

Example: {!custom field_name1}{!custom field_name2}{!custom field_name3}

I hope this helps in your use of the Aweber API! Let me know if you have any questions in the comments section below.

Share on FacebookTweet about this on TwitterShare on Google+

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>