Aweber integration with WordPress, a perfect combination to promote email marketing using your WordPress site. This post shows how to integrate the aweber plugin on your WordPress website and useful guide to get authentication variables of Aweber API.To get started, You need the following information to communicate with aweber account using PHP.
  • consumerKey
  • consumerSecret
  • accessKey
  • accessSecret

ConsumerKey and ConsumerSecret in aweber api, are properties of the application while accessKey and accessSecret are authorization properties. You must need to authorize your application with aweber account to get accessKey and accessSecret.

Create A New App on Aweber

To create an aweber application, you should have a developer account on aweber lab which is totally different from a normal aweber account. After logging in from your developer account, go to the apps page.

create a new app on aweber
create a new app on aweber

Get ConsumerKey and ConsumerSecret from Aweber Application

Aweber apps page display list of applications created by the user and display consumerKey and consumerSecret as well.

get-consumer-key-and-consumer-secret-aweber-integration
get consumerKey and consumerSecret for aweber integration

Get accessKey and accessSecret from Aweber Authentication

Get accessKey and accessSecret using aweber authorization was always a confusing chapter for me because of lack of well written Aweber documentation. Once authorization completed, an authorization code displayed on your screen which has 5 elements separated by pipe (|).

get authorization code in aweber integration

To authorize your application to connect your Aweber account use following code snippets.

<?php
require_once('aweber_api/aweber_api.php');

$consumerKey = '***************'; // insert your consumerKey here
$consumerSecret = '************'; // insert your consumerSecret here

$aweber = new AWeberAPI($consumerKey, $consumerSecret);
$callbackURL    = 'https://www.flippercode.com/'; // insert your callback url here
list($key, $secret) = $aweber->getRequestToken($callbackURL);
$authorizationURL = $aweber->getAuthorizeUrl();
setcookie('secret', $secret);
header("Location: $authorizationURL");
exit();
?>
how to authorize your aweber app using php
how to authorize your aweber app using PHP

Once you have consumerKey, consumerSecret, AccessKey and accessSecret, you’re ready to go integration process. Follow these steps to integrate Aweber as a WordPress plugin using PHP.

STEP 1 – First of all, create an option under Settings.The options page must contain a form having input for following five keys

  • consumerKey
  • consumerSecret
  • accessKey
  • accessSecret
  • list_id

// list id where your emails will be subscribed.

STEP 2 – Next we need to call an action hook when a user is registered.

add_action('user_register', 'aweber_registration');

This action hook allows you to access data for a new user immediately after they are added to the database. The user id is passed to hook as an argument.

function aweber_registration($user_id)

The function ‘aweber_registration’ is run during WordPress ‘user_register’ action.

STEP 3- Here the definition of ‘aweber_registration’ function.

  Include 'aweber_api.php' file
require_once('aweber_api/aweber_api.php');

Download aweber_api zip file from https://github.com/aweber/AWeber-API-PHP-Library

2. Retrieve user details like email, first name and last name from the database.

$result = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE ID = $user_id");
		$user_email = $result->user_email;
		$fname = $result->fname;
$lname = $result->lname;

3. Get option values of options page from the database .

$consumerKey=get_option('consumer_key');
		$consumerSecret=get_option('consumer_secret_key');
		$accessKey=get_option('access_key');
		$accessSecret=get_option('access_secret_key');
		$list_id=get_option('list_id');

4. Create an object using Aweber API PHP library.

 $aweber = new AWeberAPI($consumerKey, $consumerSecret);

5. Add following code under try statement.

		
try {
			/*Get the account id first.*/
			$account = $aweber->getAccount($accessKey, $accessSecret); 
			$account_id = $account->id; 
			/*Next we get the list url.*/
			$listURL = "/accounts/{$account_id}/lists/{$list_id}"; 
/*LoadfromURL should take a relative URL and return the correct object based on that request.*/
		   	$list = $account->loadFromUrl($listURL);
			/*Create an array having following details .*/
		   	 $params = array( 
				'email' => $user_email,
				'ip_address' => $_SERVER["REMOTE_ADDR"],
				'misc_notes' => 'Registered WP USER', 
				'name' => $fname.' '.$lname 
			); 
			/*Pass  above array as an argument to create a new subscriber in the list.*/		
			$subscribers = $list->subscribers; 
			$new_subscriber = $subscribers->create($params);	
		}

6. Add following code under catch statement so that whenever an exception falls, the following error message is displayed.

catch(AWeberAPIException $exc) {		
print "
print "Type: $exc->type";
print "Msg : $exc->message";
print "Docs: $exc->documentation_url";
exit(1);
}

This guide is to integrate aweber API when a new user registered on your site as a WordPress plugin. You may follow same coding patterns to write up a stand alone PHP scripts for Aweber integration using PHP.