Skip to content

Instantly share code, notes, and snippets.

@bradt
Created March 31, 2015 15:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradt/feb6a6e3699cbec3b937 to your computer and use it in GitHub Desktop.
Save bradt/feb6a6e3699cbec3b937 to your computer and use it in GitHub Desktop.
Sync subscribers from Campaign Monitor to Drip
#!/usr/bin/php
<?php
set_time_limit(0);
// Get the Drip API wrapper at https://github.com/DripEmail/drip-php
require __DIR__ . '/Drip_API.class.php';
$drip = new Drip_API( 'xxxxxxxxxxxxxx' );
$drip_account_id = 0000000;
$drip_campaign_dev_tips = 0000000;
$drip_campaign_wpmdb = 0000000;
$drip_campaign_as3cf = 0000000;
define('CS_REST_SOCKET_TIMEOUT', 30);
define('CS_REST_CALL_TIMEOUT', 30);
// Get the Campaign Monitor API wrapper at http://campaignmonitor.github.io/createsend-php/
require_once __DIR__ . '/cm/csrest_lists.php';
require_once __DIR__ . '/cm/csrest_subscribers.php';
$cm_auth = array( 'api_key' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx' );
$cm_client_id = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$cm_lists = array(
'xxxxxxxxxxxxxxxxxxxxxxxxxxxx' => array( // Advanced WordPress Development - bradt.ca
'campaigns' => array( $drip_campaign_dev_tips ),
'source' => 'on bradt.ca',
),
'xxxxxxxxxxxxxxxxxxxxxxxxxxxx' => array( // AS3CF Pro News - Inside free plugin
'campaigns' => array( $drip_campaign_as3cf ),
'source' => 'on the settings page of the Amazon S3 & CloudFront plugin',
),
'xxxxxxxxxxxxxxxxxxxxxxxxxxxx' => array( // AS3CF Pro News - WP.org
'campaigns' => array( $drip_campaign_as3cf ),
'source' => 'linked from the Amazon S3 & CloudFront plugin page on WordPress.org',
),
'xxxxxxxxxxxxxxxxxxxxxxxxxxxx' => array( // Beta Testers
'campaigns' => array( $drip_campaign_wpmdb ),
'source' => 'for beta testing WP Migrate DB Pro',
),
'xxxxxxxxxxxxxxxxxxxxxxxxxxxx' => array( // General News & Updates - deliciousbrains.com checkout
'campaigns' => array( $drip_campaign_wpmdb, $drip_campaign_as3cf, $drip_campaign_dev_tips ),
'source' => 'on the delicousbrains.com checkout',
),
'xxxxxxxxxxxxxxxxxxxxxxxxxxxx' => array( // General News & Updates - deliciousbrains.com footer
'campaigns' => array( $drip_campaign_dev_tips, $drip_campaign_as3cf, $drip_campaign_wpmdb ),
'source' => 'in the deliciousbrains.com footer',
),
'xxxxxxxxxxxxxxxxxxxxxxxxxxxx' => array( // WordPress Development and Deployment Strategy - Advanced Excerpt
'campaigns' => array( $drip_campaign_dev_tips ),
'source' => 'in the Advanced Excerpt plugin',
),
'xxxxxxxxxxxxxxxxxxxxxxxxxxxx' => array( // WordPress Development and Deployment Strategy - deliciousbrains.com blog popup
'campaigns' => array( $drip_campaign_dev_tips ),
'source' => 'on the blog at deliciousbrains.com',
),
'xxxxxxxxxxxxxxxxxxxxxxxxxxxx' => array( // WordPress Development and Deployment Strategy - Query Recorder
'campaigns' => array( $drip_campaign_dev_tips ),
'source' => 'inside the Query Recorder plugin',
),
'xxxxxxxxxxxxxxxxxxxxxxxxxxxx' => array( // WP Migrate DB Pro News - Inside free plugin
'campaigns' => array( $drip_campaign_wpmdb ),
'source' => 'on the settings page of the WP Migrate DB plugin',
),
);
foreach ( $cm_lists as $list_id => $list ) {
$page = 0;
$cm_rest_lists = new CS_REST_Lists( $list_id, $cm_auth );
$cm_rest_subscribers = new CS_REST_Subscribers( $list_id, $cm_auth );
do {
$page++;
$rest = $cm_rest_lists->get_active_subscribers( '', $page );
$response = $rest->response;
foreach ( $response->Results as $subscriber ) {
$name = trim( $subscriber->Name );
$email = trim( $subscriber->EmailAddress );
$subscriber = array(
'email' => $email,
'double_optin' => false,
'custom_fields' => array(
'source' => $list['source'],
),
);
if ( $name ) {
$pos = strpos( $name, ' ' );
if ( $pos === false ) {
$subscriber['custom_fields']['first_name'] = $name;
}
else {
$subscriber['custom_fields']['first_name'] = substr( $name, 0, $pos );
$subscriber['custom_fields']['last_name'] = substr( $name, $pos );
}
}
$subscriber['account_id'] = $drip_account_id;
$result = $drip->create_or_update_subscriber( $subscriber );
if ( ! $result ) {
echo "Could not create/update subscriber $email!\n";
print_r( $drip->get_request_info() );
}
foreach ( $list['campaigns'] as $campaign_id ) {
$subscriber['campaign_id'] = $campaign_id;
$result = $drip->subscribe_subscriber( $subscriber );
if ( ! $result && 'Email is already subscribed' != $drip->get_error_message() ) {
echo "Could not add subscriber $email to $campaign_id!\n";
print_r( $drip->get_request_info() );
}
}
$result = $cm_rest_subscribers->delete( $email );
if ( ! $result->was_successful() ) {
echo "Could not remove subscriber $email from $list_id!\n";
print_r( $result->response );
}
}
}
while ( $page < $response->NumberOfPages );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment