/** * @expectedException Sailthru_Client_Exception */ public function testSailthru_Client_Exception() { $api_key = "invalid_key"; $api_secret = "invalid_secret"; $api_url = "https://api.invalid_url.com"; $sailthruClient = new Sailthru_Client($api_key, $api_secret, $api_url); $sailthruClient->getEmail("*****@*****.**"); }
function get_all_lists() { require_once 'client/requires.php'; $client = new Sailthru_Client(get_option('sailthru_api_key'), get_option('sailthru_secret')); //@TODO: need to do an API call here //return array('426' => 'test'); //return array('pheonix', 'seed'); $lists = $client->apiGet('list', array()); $list_names = array(); foreach ($lists as $list) { if (is_array($list)) { foreach ($list as $list_name) { $list_names[] = $list_name['name']; } } } return $list_names; }
if (!empty($_POST['sailthru_email_list'])) { $lists = explode(',', $_POST['sailthru_email_list']); foreach ($lists as $key => $list) { $subscribe_to_lists[$list] = 1; } $options['lists'] = $subscribe_to_lists; } else { $options['lists'] = array('Sailthru Subscribe Widget' => 1); // subscriber is an orphan } $options['vars']['source'] = get_bloginfo('url'); $return['data'] = array('email' => $email, 'options' => $options); if ($return['error'] == false) { $sailthru = get_option('sailthru_setup_options'); $api_key = $sailthru['sailthru_api_key']; $api_secret = $sailthru['sailthru_api_secret']; $client = new Sailthru_Client($api_key, $api_secret); $res = $client->saveUser($email, $options); if ($res['ok'] != true) { $result['error'] = true; $result['message'] = "There was an error subscribing you. Please try again later."; } $return['result'] = $res; } break; default: $return['error'] = true; $return['message'] = 'No action defined. None taken.'; } echo json_encode($return); die;
public function testCustomTimeoutParameter() { $sailthru_client = new Sailthru_Client($this->api_key, $this->api_secret, $this->api_url, array('timeout' => 1, 'connect_timeout' => 2)); $this->assertEquals(1, $sailthru_client->getTimeout()); $this->assertEquals(2, $sailthru_client->getConnectTimeout()); }
foreach ($required_fields as $field => $msg) { if (!isset($_POST[$field]) || !$_POST[$field]) { $errors[] = $msg; } } if (!is_numeric($_POST['list'])) { $errors[] = 'You must select a list.'; } if ($_POST['schedule_time'] && strtotime($_POST['schedule_time']) == FALSE) { $errors[] = 'Invalid schedule time.'; } if (!count($errors)) { $lists = sailthru_form::get_all_lists(); $list = $lists[$_POST['list']]; require_once 'client/requires.php'; $client = new Sailthru_Client(get_option('sailthru_api_key'), get_option('sailthru_secret')); $r = $client->scheduleBlast($_POST['blast_name'], $list, $_POST['schedule_time'], $_POST['from_name'], $_POST['from_email'], $_POST['subject'], $_POST['content'], $_POST['plaintext_body'], array("report_email" => $_POST['report_email'])); if ($r['status'] == 'scheduled') { $errors[] = 'Your blast has been successfully scheduled for: ' . $r['schedule_time']; update_option('triggermail_last_blast', $_POST); } else { $errors[] = $r['errormsg']; } } } else { //no post.. $_POST = get_option('triggermail_last_blast'); } ?> <a href="options-general.php?page=sailthru&action=forms">Edit Forms</a> | Send an Email Blast | <a href="options-general.php?page=sailthru&action=options">General Options</a> | <a href="options-general.php?page=sailthru&action=datafeeds">Manage Datafeeds</a> <?php
<? require('_autoload.php'); $api_key = "SAILTHRU-API-KEY"; $api_secret = "SAILTHRU-API-SECRET"; $client = new Sailthru_Client($api_key, $api_secret); try { // update existing user by Sailthru ID $response = $client->saveUser("4e2879472d7acd6d97144f9e", array( 'keys' => array( 'email' => '*****@*****.**', 'twitter' => 'infynyxx', 'fb' => 726310296 ), 'lists' => array( 'list-1' => 1, 'list-2' => 1, 'list-3' => 0 ) )); //update existing user by email or create new user using email address $response = $client->saveUser('*****@*****.**', array( 'key' => 'email', 'lists' => array( 'list-1' => 0 // optout from list-1 ) )); // get user by Sailthru ID
} else { require_once 'client/requires.php'; $client = new Sailthru_Client(get_option('sailthru_api_key'), get_option('sailthru_secret')); $replacement_fields = array(); if (isset($_POST['fname'])) { $replacement_fields['first_name'] = $_POST['fname']; } if (isset($_POST['lname'])) { $replacement_fields['last_name'] = $_POST['lname']; } $all_lists = $form->get_all_lists(); $lists = array(); if ($has_lists) { foreach ($all_lists as $id => $list_name) { if (in_array($id, $_POST['lists'])) { $lists[$list_name] = '1'; } } } $client->setEmail($_POST['email'], $replacement_fields, $lists); if ((bool) get_option('sailthru_welcome') && ($template = get_option('sailthru_welcome_template'))) { require_once 'client/requires.php'; $client = new Sailthru_Client(get_option('sailthru_api_key'), get_option('sailthru_secret')); $r = $client->send($template, $_POST['email'], $replacement_fields, array()); } echo '{}'; } } break; } die;
/** * Recent Campaigns - This function is used for AJAX requests to obtain information on * recent campaigns, or a set of campaigns filtered by date and/or list. * * @param * start_date (optional) - The start date of the campaign information. * Defaults to 7 days before end date, or 7 days before current date if end date not set. * end_date (optional) - The end date of the campaign information. * Defaults to 7 days after the start date, or the current date if start date not set. * list (optional) - The list used to filter campaigns by. Defaults to all lists. * stat_1 - The first statistic queried for the set of campaigns (e.g. open %, click %, bounce % ). * stat_2 - The second statistic queried for the set of campaigns (e.g. open %, click %, bounce % ). * page - the page to request information for. * @return * Returns a table of the information requested in html form. */ public function reports_recent_campaigns() { $sailthruClient = new Sailthru_Client(API_KEY, API_SECRET); // If both start and end date are not set, set to default values. if ($this->params['pass'][0] == 'null' && $this->params['pass'][1] == 'null') { $end_date = date("m/d/y"); $temp = strtotime($end_date) - DEFAULT_DAYS * 86400; // 86400 = seconds in a day $start_date = date("m/d/y", $temp); // If start is not set, default it to 7 days prior to end date. } else { if ($this->params['pass'][0] == 'null') { $temp = strtotime($this->params['pass'][1]); $end_date = date("m/d/y", $temp); $temp = strtotime($end_date) - DEFAULT_DAYS * 86400; $start_date = date("m/d/y", $temp); // If end date is not set, default it to 7 days after the start date. } else { if ($this->params['pass'][1] == 'null') { $temp = strtotime($this->params['pass'][0]); $start_date = date("m/d/y", $temp); $temp = strtotime($start_date) + DEFAULT_DAYS * 86400; $end_date = date("m/d/y", $temp); // Else, both days are set. Format them for the API call. } else { $temp = strtotime($this->params['pass'][0]); $start_date = date("m/d/y", $temp); $temp = strtotime($this->params['pass'][1]); $end_date = date("m/d/y", $temp); } } } //str_replace("-","/",$end_date); //str_replace("-","/",$start_date); // Check if parameters for the first statistics is set. Default to "Clicks". if ($this->params['pass'][3] == 'null') { $stat_1 = 'click'; } else { $stat_1 = $this->params['pass'][3]; } // Check if parameters for the second statistics is set. Default to "Est. Opens". if ($this->params['pass'][4] == 'null') { $stat_2 = 'estopens'; } else { $stat_2 = $this->params['pass'][4]; } // If the set start date is not before the end date chronologically, return with an error message. if (strtotime($start_date) > strtotime($end_date)) { echo 'Invalid date range.'; $this->autoLayout = $this->autoRender = false; return; } //Prepare array of options to be passed into the API call. $options['start_date'] = $start_date; $options['end_date'] = $end_date; $options['status'] = 'sent'; // If list is specified, add it to the options array. if ($this->params['pass'][2] != 'null') { $options['list'] = $this->params['pass'][2]; } try { //Retrieve the blast id via API call. $response = $sailthruClient->getBlasts($options); $results = $response['blasts']; // Calculate the number pages by dividing the total blasts by the results per page constant. $total_count = count($results); $pages = ceil($total_count / RESULTS_PER_PAGE); // Get the page requested from the URL of this request. if ($this->params['pass'][5] == 'null') { $page = 1; } else { $page = $this->params['pass'][5]; } $start = ($page - 1) * RESULTS_PER_PAGE; $end = $page * RESULTS_PER_PAGE; if (!isset($response['error'])) { // Prepare option array for secondary API call. $data['beacon_times'] = 1; $data['click_times'] = 1; $data['engagement'] = 1; $toReturn = array(); // For each of the blasts on the current page for ($i = $start; $i < $end; $i++) { if (array_key_exists($i, $results)) { $toReturn[$i]['name'] = $results[$i]['name']; $toReturn[$i]['blast_id'] = $results[$i]['blast_id']; $toReturn[$i]['list'] = $results[$i]['list']; //Make the secondary API call to retrieve the stats specified by the $stats_1 and $stats_2 parameters $blast_stats = $sailthruClient->stats_blast($results[$i]['blast_id'], null, null, $data); $toReturn[$i]['stat_1'] = isset($blast_stats[$stat_1]) ? $blast_stats[$stat_1] : 0; $toReturn[$i]['stat_2'] = isset($blast_stats[$stat_2]) ? $blast_stats[$stat_2] : 0; } } if (count($toReturn) == 0) { echo "Nothing to Display."; } //set variables and render view. $test = $start_date; $test2 = $end_date; $this->set('test', $test); $this->set('test2', $test2); $this->set('page', $page); $this->set('pages', $pages); $this->set('results', array_reverse($toReturn)); $this->layout = 'campaign_table'; //this layout simply echos the content of the View. $this->render('reports_recent_campaigns'); } else { echo 'error'; } } catch (Sailthru_Client_Exception $e) { echo $e->getMessage(); } }
function get_template_list() { require_once 'client/requires.php'; $client = new Sailthru_Client(get_option('sailthru_api_key'), get_option('sailthru_secret')); $res = $client->getTemplates(); if (isset($res['templates']) && $res['templates']) { $return = array(); foreach ($res['templates'] as $template) { $return[] = $template['name']; } return $return; } else { return array(); } }
<?php require '_autoload.php'; $api_key = "SAILTHRU-API-KEY"; $api_secret = "SAILTHRU-API-SECRET"; $client = new Sailthru_Client($api_key, $api_secret); try { // update existing user by Sailthru ID $response = $client->saveUser("4e2879472d7acd6d97144f9e", array('keys' => array('email' => '*****@*****.**', 'twitter' => 'infynyxx', 'fb' => 726310296), 'lists' => array('list-1' => 1, 'list-2' => 1, 'list-3' => 0))); //update existing user by email or create new user using email address $response = $client->saveUser('*****@*****.**', array('key' => 'email', 'lists' => array('list-1' => 0))); // get user by Sailthru ID $fields = array('keys' => 1, 'vars' => 1, 'activity' => 1); $response = $client->getUseBySid("4e2879472d7acd6d97144f9e", $fields); // get user by Custom key $response = $client->getUserByKey("*****@*****.**", 'email', $fields); // get last rate limit info $rate_limit_info = $client->getLastRateLimitInfo("user", "GET"); } catch (Sail_Client_Exception $e) { // deal with exception }