Created
June 8, 2013 20:57
-
-
Save martynchamberlin/5736557 to your computer and use it in GitHub Desktop.
Code for moving subscribers from one list to another in AWeber.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php session_start(); ?> | |
<form action="" method="post"> | |
<div> | |
<label>Old list name</label> | |
<input type="text" name="list_name" <?php if (isset($_SESSION['list_name'] )) echo 'value="' . $_SESSION['list_name'] . '"'; ?> | |
</div> | |
<div> | |
<label>New list name</label> | |
<input type="text" name="destination_list_name" <?php if (isset($_SESSION['destination_list_name'] )) echo 'value="' . $_SESSION['destination_list_name'] . '"'; ?> | |
</div> | |
<div> | |
<input type="submit" value="Move Subscribers!"> | |
</div> | |
</form> | |
<?php | |
if ( isset( $_POST['destination_list_name'] ) ) | |
{ | |
foreach ($_POST as $key=>$val ) | |
{ | |
$_SESSION[$key] = $val; | |
} | |
/** | |
* In order to include this file, you'll need to download the AWeber API | |
* at https://github.com/aweber/AWeber-API-PHP-Library | |
*/ | |
require_once('aweber_api/aweber_api.php'); | |
/** | |
* I'll let you figure out how to get these 4 values. It's not that hard, | |
* and luckily AWeber has pretty good documentation | |
*/ | |
$consumerKey = "***********"; | |
$consumerSecret = "***********"; | |
$accessToken = '***********'; | |
$accessTokenSecret = '***********'; | |
$aweber = new AWeberAPI($consumerKey, $consumerSecret); | |
$account = $aweber->getAccount($accessToken, $accessTokenSecret); | |
$account_id = $account->id; | |
$list_name = $_POST['list_name']; | |
$destination_list_name = $_POST['destination_list_name']; | |
$foundLists = $account->lists->find(array('name' => $list_name)); | |
$list = $foundLists[0]; | |
$subscribers = $list->subscribers; | |
$params = array('status' => 'subscribed'); | |
$found_subscribers = $subscribers->find($params); | |
$destination_found_lists = $account->lists->find(array('name' => $destination_list_name)); | |
$destination_list = $destination_found_lists[0]; | |
/** | |
* Store the destination list email addresses in a session array. | |
* Having it in a session array saves API calls (we're limited to 60 per minute, remember) | |
* and it keeps us from creating "duplicate email" exceptions during the move() function | |
*/ | |
if ( ! isset( $_SESSION[$destination_list_name] ) ) | |
{ | |
$subscribers_in_destination = $destination_list->subscribers; | |
$subscribers_in_destination = $subscribers_in_destination->find(array()); | |
$_SESSION['duplicates'] = array(); | |
foreach ( $subscribers_in_destination as $subscriber ) | |
{ | |
array_push( $_SESSION['duplicates'], $subscriber->email ); | |
} | |
} | |
foreach($found_subscribers as $subscriber) | |
{ | |
if ( ! in_array( $subscriber->email, $_SESSION['duplicates'] ) ) | |
{ | |
try | |
{ | |
$subscriber->move($destination_list, 1001); | |
echo '<li>' . $subscriber->email . '</li>'; | |
} | |
catch(AWeberAPIException $exc) | |
{ | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment