Skip to content

Instantly share code, notes, and snippets.

@aliboy08
Created February 22, 2018 00:36
Show Gist options
  • Save aliboy08/ca38685d33c017609bceca9cb975218e to your computer and use it in GitHub Desktop.
Save aliboy08/ca38685d33c017609bceca9cb975218e to your computer and use it in GitHub Desktop.
<?php
function ff_aweber_mass_update_fields(
$file = '',
$list_id = 2534516, // thedigitalinsurer
$consumerKey = 'AkFCmZ046GCL17wKVR0L2xsV',
$consumerSecret = 'Sa5Ugpm40NHMcIWKC6TGeDEl204JThMy0nBHFXc4',
$accessKey = 'AgzYa7KUgu0UBivqDk22tJdS',
$accessSecret = 'XmLHVT4mW1oKI5VwWIEBSGhWvfavaxja2zQZn8TN'
)
{
pre_debug($file);
pre_debug('=== Processing aweber mass update ===');
// Process CSV File
$row = 0;
if (($handle = fopen($file, "r")) !== FALSE) :
require_once(get_stylesheet_directory() .'/lib/aweber_api/aweber_api.php');
$aweber = new AWeberAPI($consumerKey, $consumerSecret);
try {
$account = $aweber->getAccount($accessKey, $accessSecret);
$account_id = $account->id;
$listURL = "/accounts/{$account_id}/lists/{$list_id}";
$list = $account->loadFromUrl($listURL);
$subscribers = $list->subscribers;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) : $row++;
if( !$data[0] || $row == 1 ) continue; // skip headers
$email = $data[0];
$fields = explode('|', trim($data[1]));
//pre_debug($subscribers);
// Find email on the list
$search_subscribers = $subscribers->find( array('email' => $email) );
foreach($search_subscribers as $subscriber) {
$have_changes = false;
//pre_debug($subscriber->data);
// Update custom fields
foreach( $fields as $field ) {
$f = explode('=', trim($field));
$key = $f[0];
$value = $f[1];
if( $subscriber->custom_fields[$key] == $value ) continue; // no change
$subscriber->custom_fields[$key] = $value;
$have_changes = true;
}
// Only save if there are changes
if( $have_changes ){
$subscriber->save();
pre_debug($email. ' updated');
}
}
endwhile;
} catch(AWeberAPIException $exc) {
print "<h3>AWeberAPIException:<h3>";
print " <li> Type: $exc->type <br>";
print " <li> Msg : $exc->message <br>";
print " <li> Docs: $exc->documentation_url <br>";
print "<hr>";
exit(1);
}
endif;
pre_debug('=== DONE ===');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment