PHP5

View on Github | Download

Constructor

$api_key = "api_key";
$api_secret = 'secret';
$sailthruClient = new Sailthru_Client($api_key, $api_secret);

### Generic Example
try {
    $response = $sailthruClient->getSend("TUMVqWdj2exnAAV-");
    if ( !isset($response['error']) ) {
        // everything OK
        // do something here
    } else {
        // handle API error
    }
} catch (Sailthru_Client_Exception $e) {
    // deal with exceptions
}

send

//send
$response = $sailthruClient->send('template-name', 'praj@sailthru.com', array('name' => 'unix'), array('test' => 1));

//multi send
$response = $sailthruClient->multisend('default', 'abc@xyz.com,praj@sailthru.com', array('name' => 'unix'), array(), array('test' => 1));

//get send
$response = $sailthruClient->getSend("TUMVqWdj2exnAAV-");

//cancel send
$response = $sailthruClient->cancelSend("TUMYT2dj2fl1AABD");

blast

//Get Blast information
$blast_id = 52424;
$response = $sailthruClient->getBlast($blast_id);

//schedule blast
$blast_name = 'test_blast1';
$list = 'default';
$schedule_time = '+1 days';
$from_name = 'Prajwal Tuladhar';
$from_email = 'praj@sailthru.com';
$subject = "Hey what's up!";
$content_html = "<b>Lorem ipsum dolor si</b>";
$content_text = strip_tags($content_html);

$response = $sailthruClient->scheduleBlast($blast_name, $list, $schedule_time, $from_name, $from_email, $subject, $content_html, $content_text);

//schedule blast from template
$template = 'default';
$list = 'default';
$schedule_time = 'now';
$options  = array();
#$response = $sailthruClient->scheduleBlastFromTemplate($template, $list, $schedule_time, $options);

//schedule blast from previous blast
//Note: if blast_id is invalid, request won't work
$_blast_id = '110065';
$_schedule_time = 'now';
$_options = array(
'vars' => array(
    'my_var' => '3y6366546363',
    'my_var2' => array(7,8,9),
    'my_var3' => array('president' => 'obama', 'nested' => array('vp' => 'palin'))),
);
$response = $sailthruClient->scheduleBlastFromBlast($_blast_id, $_schedule_time, $_options);

//update blast
$blast_id = 46513;
$blast_name = 'test_blast88';
$response = $sailthruClient->updateBlast($blast_id, $blast_name, $list, $schedule_time, $from_name, $from_email, $subject, $content_html, $content_text);

//cancel scheduled blast
$response = $sailthruClient->cancelBlast($blast_id);

//delete blast
$response = $sailthruClient->deleteBlast($blast_id);

list

//get metadata for all lists
$lists_metadata = $sailthruClient->getLists();

//download a list
$list = 'default';
$response = $sailthruClient->getList($list, "txt");

//saves /updates a list
$emails = 'more@abc.com, less@xyz.com';
$response = $sailthruClient->saveList($list, $emails);

//delete a list
$response = $sailthruClient->deleteList($list);

template

//get meta-data of all exisiting templates
$response = $sailthruClient->getTemplates();

//get information of a given template
$template = 'welcome-template';
$response = $sailthruClient->getTemplate($template);

//get information of a template from it's revision id
$revision_id = 45204;
$response = $sailthruClient->getTemplateFromRevision($revision_id);

//create new template or update existing one
$template = 'new-template';
$options = array(
'from_name' => 'Sailthru Support',
'from_email' => 'support@sailthru.com',
'content_html' => '<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec qu</p>',
'subject' => 'Sailthru Support'
);
$response = $sailthruClient->saveTemplate($template, $options);

//save template from existing revision id
$template = 'welcome-template';
$revision_id = 45204;
$response = $sailthruClient->saveTemplateFromRevision($template, $revision_id);

//delete existing template
$template = 'welcome-template';
$response = $sailthruClient->deleteTemplate($template);

content

//push content
$title = 'hello world';
$url = 'http://example.com/product-url';
$response = $sailthruClient->pushContent($title, $url);

//another push content example
$title = 'Widget';
$url = 'http://example.com/product-url';
$date = null;
$tags = array("blue", "red", "green");
$vars = array('vars' => array('category' => 'spring sale'));
$options = array('price' => 9999, 'expire_date' => '2014-01-01 00:00:00 -0500');
$response = $sailthruClient->pushContent($title, $url, $date, $tags, $vars, $options);

purchase

//post purchase
$email = 'praj@sailthru.com';
$items = array(
    array('id' => 11, 'price' => 26262, 'qty' => '11', 'url' => 'http://example.com/234/high-impact-water-bottle', 'title' => 'High-Impact Water Bottle'),
    array('id' => 171, 'price' => 262, 'qty' => '18', 'url' => 'http://xyz.com/abc', 'title' => 'some title2')
);
$response = $sailthruClient->purchase($email, $items);

//post purchase incomplete request
$email = 'praj@sailthru.com';
$items = array(
    array('id' => 11, 'price' => 26262, 'qty' => '11', 'url' => 'http://example.com/234/high-impact-water-bottle', 'title' => 'High-Impact Water Bottle'),
    array('id' => 171, 'price' => 262, 'qty' => '18', 'url' => 'http://xyz.com/abc', 'title' => 'some title2')
);
$message_id = '23423.231';
$response = $sailthruClient->purchaseIncomplete($email, $items, $message_id);

stats

//get list stats
$response = $sailthruClient->stats_list();

//get blast stats
$blast_id = 6752;
$response = $sailthruClient->stats_blast($blast_id);

Personalization Engine

//sets Personalization Engine (formerly known as Horizon) user data
$email = 'praj@sailthru.com';
$tags = array('blue', 'red', 'green');
$response = $sailthruClient->setHorizon($email, $tags);

//set Personalization Engine (Horizon) cookie
$email = 'praj@sailthru.com';
$sailthruClient->setHorizonCookie($email);

job

//get the status of a job
$job_id = '4dd58f036803fa3b5500000b';
$response = $sailthruClient->getJobStatus($job_id);

# process import job for email string
$list = 'test-list';
$emails = 'a@a.com,b@b.com';
$response = $sailthruClient->processImportJob($list, $emails);

# process import job from CSV or text file
$list = 'test-list';
$source_file = '/home/praj/Desktop/emails.txt';
$response = $sailthruClient->processImportJobFromFile($list, $source_file);

# process snapshot job
$query = array();
$report_email = 'praj@sailthru.com';
$postback_url = 'http://example.com/reports/snapshot_postback';
response = $sailthruClient->processSnapshotJob($query);

# process export list job
$list = 'test-list';
$response = $sailthruClient->processExportListJob($list);

# process update job from emails CSV
$emails = "praj@sailthru.com,ian@sailthru.com";
$update = array(
'lists' => array('sailthru-list' => 1),
'vars' => array('location' => 'New York, NY')
);
$response = $sailthruClient->processUpdateJobFromEmails($emails, $update);

API Postbacks

//recieve verify post
$sailthruClient->receiveVerifyPost();

//recieve optout post
$sailthruClient->receiveOptoutPost();

Rate Limits

// get last rate limit info
$rate_limit_info = $sailthru_client->getLastRateLimitInfo("user", "POST");

// getRateLimitInfo returns null if given endpoint/method wasn't triggered previously
if ($rate_limit_info) {
    $limit = $rate_limit_info['limit'];
    $remaining = $rate_limit_info['remaining'];
    $reset_timestamp = $rate_limit_info['reset'];

    // throttle api calls based on last rate limit info
    if ($remaining <= 0) {
        $seconds_till_reset = $reset_timestamp - time();

        // sleep or perform other business logic before next user api call
        sleep($seconds_till_reset);
    } 
}
Top