/** * Returns all clients accessible with the current api key * * @return ArrayList[CMClient] * @throws CMError */ function Clients() { $interface = new CS_REST_General($this->apiKey); $result = $interface->get_clients(); $response = $this->parseResult($result); // Save each client $clients = new ArrayList(); foreach ($response as $clientData) { $clients->push(new CMClient($this->apiKey, $clientData)); } return $clients; }
/** * Get List from Campaign Monitor */ function cspv4_get_campaignmonitor_clients($apikey = null) { global $seed_cspv4; extract($seed_cspv4); $o = $seed_cspv4; $clients = array(); if ($o['emaillist'] == 'campaignmonitor' || defined('DOING_AJAX') && DOING_AJAX && isset($_GET['action']) && $_GET['action'] == 'seed_cspv4_campaingmonitor_client') { $clients = maybe_unserialize(get_transient('seed_cspv4_campaignmonitor_clients')); if (empty($clients)) { //var_dump('miss'); if (class_exists('CS_REST_General')) { //trigger_error("Duplicate: Another Campaign Moniter client library is already in scope.", E_USER_WARNING); } else { require_once SEED_CSPV4_PLUGIN_PATH . 'extentions/campaignmonitor/campaign_monitor/csrest_general.php'; } if (!isset($apikey) && isset($campaignmonitor_api_key)) { $apikey = $campaignmonitor_api_key; } if (empty($apikey)) { return array(); } $api = new CS_REST_General($apikey); $response = $api->get_clients(); if ($response->was_successful()) { foreach ($response->response as $k => $v) { $clients[$v->ClientID] = $v->Name; } if (!empty($clients)) { set_transient('seed_cspv4_campaignmonitor_clients', serialize($clients), 86400); } } else { $clients['false'] = __("Unable to load Campaign Monitor clients", 'seedprod'); } } } return $clients; }
function tfuse_ajax_newsletter_save_campaignmonitor_api_key() { #CampaignMonitor tf_can_ajax(); require_once dirname(__FILE__) . '/library/csrest_general.php'; $api_key = $this->request->POST('api_key'); $api = new CS_REST_General($api_key); $result = $api->get_clients(); if ($result->http_status_code == 401) { tfjecho(array('status' => -1, 'message' => __('API key is invalid.', 'tfuse'))); } else { $this->model->set_cm_key($api_key); tfjecho(array('status' => 1)); } die; }
<?php require_once '../csrest_general.php'; $wrap = new CS_REST_General('Your API Key'); $result = $wrap->get_countries(); echo "Result of /api/v3/countries\n<br />"; if ($result->was_successful()) { echo "Got countries\n<br /><pre>"; var_dump($result->response); } else { echo 'Failed with code ' . $result->http_status_code . "\n<br /><pre>"; var_dump($result->response); } echo '</pre>';
<?php require_once '../csrest_general.php'; $wrap = new CS_REST_General('Your API Key'); $result = $wrap->get_clients(); echo "Result of /api/v3/clients\n<br />"; if ($result->was_successful()) { echo "Got clients\n<br /><pre>"; var_dump($result->response); } else { echo 'Failed with code ' . $result->http_status_code . "\n<br /><pre>"; var_dump($result->response); } echo '</pre>';
<?php require_once '../csrest_general.php'; $auth = array('access_token' => 'your access token', 'refresh_token' => 'your refresh token'); $wrap = new CS_REST_General($auth); $result = $wrap->get_timezones(); echo "Result of /api/v3.1/timezones\n<br />"; if ($result->was_successful()) { echo "Got timezones\n<br /><pre>"; var_dump($result->response); } else { echo 'Failed with code ' . $result->http_status_code . "\n<br /><pre>"; var_dump($result->response); } echo '</pre>';
require_once "functions.php"; //define api_key and cliend_id that I get by my subscription to the API define("API_KEY", "f955097a9e86fcab60f3bbbdcf97e6b9"); define("CLIENT_ID", "df0a4e647a3d6d224f4a1cb95ed406be"); //Check if there are parameters in the server url //If the parameter req(request) isset, we have a request from the client //Mainly Ajax requests $req = isset($_GET["req"]) ? $_GET["req"] : ""; $auth = array('api_key' => API_KEY); //An if statement checks which action has server to perform //according to the parameters of the url if ($req == "viewLists") { //View available client's Lists //Get the appropriate wrapper to check //if there is the given CLIENT_ID $wrap_general = new CS_REST_General($auth); $clients = $wrap_general->get_clients(); foreach ($clients->response as $client) { if (CLIENT_ID == $client->ClientID) { $client_id = CLIENT_ID; break; } else { echo "There is not such a client id!"; } } //If client found if (isset($client_id)) { //Get the appropriate wrapper to get the available client's Lists $wrap_client = new CS_REST_Clients($client_id, $auth); $lists = $wrap_client->get_lists(); //JSON manipiulation of the response
/** * Get url for authentification on Instagram * @return string */ protected function _getAuthUrl() { $url = CS_REST_General::authorize_url($this->_getClientId(), $this->_getAuthRedirectUri(), 'ImportSubscribers,ManageLists'); return $url; }
<?php require_once '../csrest_general.php'; $auth = array('access_token' => 'your access token', 'refresh_token' => 'your refresh token'); $wrap = new CS_REST_General($auth); $result = $wrap->get_billing_details(); echo "Result of /api/v3.1/billingdetails\n<br />"; if ($result->was_successful()) { echo "Got billing details\n<br /><pre>"; var_dump($result->response); } else { echo 'Failed with code ' . $result->http_status_code . "\n<br /><pre>"; var_dump($result->response); } echo '</pre>';
<?php require_once '../../csrest_general.php'; $client_id = 8998879; $redirect_uri = 'http://example.com/auth'; $scope = 'ViewReports,CreateCampaigns,SendCampaigns'; $state = 'some state data'; $authorize_url = CS_REST_General::authorize_url($client_id, $redirect_uri, $scope, $state); echo "Redirect your users to: " . $authorize_url . "\n<br />";
/** * Validate the key. * * @return bool|null */ public function is_valid_key($key) { if (empty($key)) { return null; } $this->include_api(); $api = new CS_REST_General($key); $result = $api->get_systemdate(); return $result->was_successful(); }
protected function getClientApiId() { require_once 'lib/csrest_general.php'; $wrap = new CS_REST_General(array('api_key' => $this->getConfig('api_key'))); $result = $wrap->get_clients(); if ($result->was_successful()) { $res = array(); foreach ($result->response as $r) { $res[$r->ClientID] = $r->Name; } return $res; } $this->getDi()->errorLogTable->log('Campaignmonitor ERROR: Bad API Key'); }
public function refreshToken() { // Check if auth type is OAuth if ($this->isOAuth()) { // Get the credentials $accessToken = Mage::getModel('campaignmonitor/auth')->getAccessToken(); $refreshToken = Mage::getModel('campaignmonitor/auth')->getRefreshToken(); $auth = array('access_token' => $accessToken, 'refresh_token' => $refreshToken); // Use the REST lib to refresh the token $wrap = new CS_REST_General($auth); list($new_access_token, $new_expires_in, $new_refresh_token) = $wrap->refresh_token(); // Use stdClass as it's the same type as OG response $response = new stdClass(); $response->access_token = $new_access_token; $response->expires_in = $new_expires_in; $response->refresh_token = $new_refresh_token; $session = Mage::getModel('core/session'); $session->setData(self::CAMPAIGNMONITOR_SESSION_DATA_KEY, $response); // Save $new_access_token, $new_expires_in, and $new_refresh_token Mage::getConfig()->saveConfig(self::CAMPAIGNMONITOR_CONFIG_DATA_KEY, serialize($response), 'default', 0); } }
<?php require_once '../csrest_general.php'; $auth = array('access_token' => 'your access token', 'refresh_token' => 'your refresh token'); $wrap = new CS_REST_General($auth); $result = $wrap->external_session_url(array('Email' => 'The email address of the Campaign Monitor user for whom the login session should be created', 'Chrome' => 'Which chrome to display - Must be either "all", "tabs", or "none"', 'Url' => 'The URL to display once logged in. e.g. "/subscribers/"', 'IntegratorID' => 'The Integrator ID. You need to contact Campaign Monitor support to get an Integrator ID.', 'ClientID' => 'The Client ID of the client which should be active once logged in to the Campaign Monitor account.')); echo "Result of PUT /api/v3.1/externalsession\n<br />"; if ($result->was_successful()) { echo "Succeeded with Code " . $result->http_status_code . "\n<br /><pre>"; var_dump($result->response); echo '</pre>'; } else { echo 'Failed with code ' . $result->http_status_code . "\n<br /><pre>"; var_dump($result->response); echo '</pre>'; }
<?php require_once '../../csrest_general.php'; $auth = array('access_token' => 'your access token', 'refresh_token' => 'your refresh token'); $wrap = new CS_REST_General($auth); $result = $wrap->get_clients(); if (!$result->was_successful()) { # If you receive '121: Expired OAuth Token', refresh the access token if ($result->response->Code == 121) { list($new_access_token, $new_expires_in, $new_refresh_token) = $wrap->refresh_token(); # Save $new_access_token, $new_expires_in, and $new_refresh_token } # Make the call again $result = $wrap->get_clients(); } var_dump($result->response);
<?php require_once '../csrest_general.php'; $wrap = new CS_REST_General(NULL); $result = $wrap->get_apikey('Your username', 'Your password', 'account.test.createsend.com'); echo "Result of /api/v3.1/apikey\n<br />"; if ($result->was_successful()) { echo "Got API Key\n<br />" . $result->response->ApiKey; } else { echo 'Failed with code ' . $result->http_status_code . "\n<br /><pre>"; var_dump($result->response); echo '</pre>'; }
/** * Render the markup for service specific fields. * * @since 1.5.4 * @param string $account The name of the saved account. * @param object $settings Saved module settings. * @return array { * @type bool|string $error The error message or false if no error. * @type string $html The field markup. * } */ public function render_fields($account, $settings) { $post_data = FLBuilderModel::get_post_data(); $account_data = $this->get_account_data($account); $api = new CS_REST_General($account_data); $result = $api->get_clients(); $response = array('error' => false, 'html' => ''); if ($result->was_successful()) { if (!isset($post_data['client'])) { $response['html'] .= $this->render_client_field($result, $settings); } $response['html'] .= $this->render_list_field($account_data, $settings); } else { $response['error'] = __('Error: Please check your API key.', 'fl-builder'); } return $response; }
/** * Retrieval method for getting clients. * * @since 2.0.0 * * @param array $args Args to be passed for list retrieval. * @param string $client_id The client ID to check for selection. * @param string $list_id The list ID to target. * @param string $uniqid The account ID to target. * @param string $optin_id The optin ID to target. * * @return string|WP_Error Output of the clients or WP_Error. */ public function get_clients($args = array(), $client_id = '', $list_id = '', $uniqid = '', $optin_id = '') { if (!$this->api) { if (isset($args['access']) && isset($args['refresh']) && isset($args['expires'])) { $this->api['access'] = $args['access']; $this->api['refresh'] = $args['refresh']; $this->api['expires'] = $args['expires']; } } // If needed, get a new access token. if (isset($this->api['expires']) && isset($this->api['refresh']) && time() > $this->api['expires']) { $this->api['access'] = $this->refresh_access_token($uniqid, $this->api['refresh']); } // Possibly get data via OAuth or API key for backwards compat. if (isset($this->api['access']) && isset($this->api['refresh'])) { $auth = array('access_token' => $this->api['access'], 'refresh_token' => $this->api['refresh']); } else { if (isset($args['api'])) { $auth = array('api_key' => $args['api']); } else { $auth = array(); } } $api = new CS_REST_General($auth); $clients = $api->get_clients(); $output = ''; $output .= '<div class="optin-monster-field-box optin-monster-provider-clients optin-monster-clear">'; $output .= '<p class="optin-monster-field-wrap"><label for="optin-monster-client-list">' . __('Campaign Monitor Client', 'optin-monster') . '</label><br />'; $output .= '<select id="optin-monster-client-list" name="optin_monster[provider_client]">'; foreach ($clients->response as $client) { $output .= '<option value="' . $client->ClientID . '"' . selected($client_id, $client->ClientID, false) . '>' . $client->Name . '</option>'; } $output .= '</select>'; $output .= '</p>'; $output .= '</div>'; // Get the lists for the first client. $this->api['client'] = !empty($client_id) ? $client_id : $clients->response[0]->ClientID; $output .= $this->get_lists($args, $list_id, $uniqid, $optin_id); return $output; }
<?php require_once '../../csrest_general.php'; $client_id = 8998879; $client_secret = 'iou0q9wud0q9wd0q9wid0q9iwd0q9wid0q9wdqwd'; $redirect_uri = 'http://example.com/auth'; $code = 'd92id09iwdwqw'; $result = CS_REST_General::exchange_token($client_id, $client_secret, $redirect_uri, $code); if ($result->was_successful()) { $access_token = $result->response->access_token; $expires_in = $result->response->expires_in; $refresh_token = $result->response->refresh_token; # Save $access_token, $expires_in, and $refresh_token. echo "access token: " . $access_token . "\n"; echo "expires in (seconds): " . $expires_in . "\n"; echo "refresh token: " . $refresh_token . "\n"; } else { echo 'An error occurred:\\n'; echo $result->response->error . ': ' . $result->response->error_description . "\n"; # Handle error... }
<?php require_once '../csrest_general.php'; $wrap = new CS_REST_General('Your API Key'); $result = $wrap->get_systemdate(); echo "Result of /api/v3/systemdate\n<br />"; if ($result->was_successful()) { echo "Got system date\n<br />" . $result->response->SystemDate; } else { echo 'Failed with code ' . $result->http_status_code . "\n<br /><pre>"; var_dump($result->response); echo '</pre>'; }
function test_static_authorize_url_with_state() { $client_id = 8998879; $redirect_uri = 'http://example.com/auth'; $scope = 'ViewReports,CreateCampaigns,SendCampaigns'; $state = 89879287; $expected_result = "https://api.createsend.com/oauth?client_id=8998879&redirect_uri=http%3A%2F%2Fexample.com%2Fauth&scope=ViewReports%2CCreateCampaigns%2CSendCampaigns&state=89879287"; $result = CS_REST_General::authorize_url($client_id, $redirect_uri, $scope, $state); $this->assertIdentical($expected_result, $result); }
private static function is_valid_key() { self::include_api(); $api = new CS_REST_General(self::get_api_key()); $result = $api->get_systemdate(); return $result->was_successful(); }
/** * Get all Campaign Monitor Lists and display in settings * */ public function get_campaignmonitor_lists() { $lists = ''; $clients = ''; if (isset($_POST['api_key']) && $_POST['api_key'] != '') { require_once( 'includes/campaignmonitor/csrest_general.php'); require_once( 'includes/campaignmonitor/csrest_clients.php'); $auth = array('api_key' => sanitize_text_field( $_POST['api_key'] )); $wrap = new CS_REST_General($auth); $result = $wrap->get_clients(); if ($result->was_successful()) { foreach ($result->response as $client) { $clients .= '<option value="' . $client->ClientID . '">' . $client->Name . '</option>'; } if (isset($_POST['client']) && $_POST['client'] != '') { $clients = ''; foreach ($result->response as $client) { if ($_POST['client'] == $client->ClientID) { $clients .= '<option value="' . $client->ClientID . '" selected="selected">' . $client->Name . '</option>'; } else { $clients .= '<option value="' . $client->ClientID . '">' . $client->Name . '</option>'; } } } if (isset($_POST['client']) && $_POST['client'] != '') { $client_id = sanitize_text_field( $_POST['client'] ); } else { $client_id = $result->response[0]->ClientID; } $wrap = new CS_REST_Clients($client_id, sanitize_text_field( $_POST['api_key']) ); $result = $wrap->get_lists(); if ($result->was_successful()) { foreach ($result->response as $list) { $lists .= '<option value="' . $list->ListID . '">' . $list->Name . '</option>'; } if (isset($_POST['list']) && $_POST['list'] != '') { $lists = ''; foreach ($result->response as $list) { if ($_POST['list'] == $list->ListID) { $lists .= '<option value="' . $list->ListID . '" selected="selected">' . $list->Name . '</option>'; } else { $lists .= '<option value="' . $list->ListID . '">' . $list->Name . '</option>'; } } } } } } echo json_encode(array('clients' => $clients, 'lists' => $lists)); die(); // this is required to terminate immediately and return a proper response }
/** * Processes campaignmonitor client field. Called by AJAX script. */ public static function field_campaignmonitor_client_ajax() { if (!class_exists(CS_REST_General)) { require CH_Manager::$plugin_dir . 'lib/campaign_monitor/csrest_general.php'; } $api_key = $_POST['apikey']; $cmapi = new CS_REST_General($api_key); $clients = $cmapi->get_clients(); if (!$clients->was_successful()) { die; } $value = ''; if (!empty($_POST['value'])) { $value = $_POST['value']; } $output = ''; $clients = $clients->response; if (is_array($clients)) { foreach ($clients as $client) { $selected = ''; if ($client->ClientID == $value) { $selected = ' selected="selected"'; } $output .= '<option value="' . esc_attr($client->ClientID) . '"' . $selected . '>' . esc_html(trim($client->Name)) . ' </option>'; } } echo $output; die; }
/** * Get all Campaign Monitor Lists and display in settings * * @access public * @return void */ function nnr_new_int_get_campaignmonitor_lists_v1() { do_action('nnr_news_int_before_get_campaignmonitor_lists_v1'); $lists = ''; $clients = ''; if (isset($_POST['api_key']) && $_POST['api_key'] != '') { require_once dirname(dirname(__FILE__)) . '/services/campaignmonitor/csrest_general.php'; require_once dirname(dirname(__FILE__)) . '/services/campaignmonitor/csrest_clients.php'; $auth = array('api_key' => $_POST['api_key']); $wrap = new CS_REST_General($auth); $result = $wrap->get_clients(); if ($result->was_successful()) { foreach ($result->response as $client) { $clients .= '<option value="' . $client->ClientID . '">' . $client->Name . '</option>'; } if (isset($_POST['client']) && $_POST['client'] != '') { $clients = ''; foreach ($result->response as $client) { if ($_POST['client'] == $client->ClientID) { $clients .= '<option value="' . $client->ClientID . '" selected="selected">' . $client->Name . '</option>'; } else { $clients .= '<option value="' . $client->ClientID . '">' . $client->Name . '</option>'; } } } if (isset($_POST['client']) && $_POST['client'] != '') { $client_id = $_POST['client']; } else { $client_id = $result->response[0]->ClientID; } $wrap = new CS_REST_Clients($client_id, $_POST['api_key']); $result = $wrap->get_lists(); if ($result->was_successful()) { foreach ($result->response as $list) { $lists .= '<option value="' . $list->ListID . '">' . $list->Name . '</option>'; } if (isset($_POST['list']) && $_POST['list'] != '') { $lists = ''; foreach ($result->response as $list) { if ($_POST['list'] == $list->ListID) { $lists .= '<option value="' . $list->ListID . '" selected="selected">' . $list->Name . '</option>'; } else { $lists .= '<option value="' . $list->ListID . '">' . $list->Name . '</option>'; } } } } } } do_action('nnr_news_int_after_get_campaignmonitor_lists_v1'); echo json_encode(apply_filters('nnr_news_int_get_campaignmonitor_lists_v1', array('clients' => $clients, 'lists' => $lists))); die; // this is required to terminate immediately and return a proper response }