/**
  * Retrieves the lists via Campaign Monitor API and updates the data in DB.
  * @return string
  */
 function get_campaign_monitor_lists($api_key, $name)
 {
     require_once RAD_RAPIDOLOGY_PLUGIN_DIR . 'subscription/createsend-php-4.0.2/csrest_clients.php';
     require_once RAD_RAPIDOLOGY_PLUGIN_DIR . 'subscription/createsend-php-4.0.2/csrest_lists.php';
     $auth = array('api_key' => $api_key);
     $request_url = esc_url_raw('https://api.createsend.com/api/v3.1/clients.json?pretty=true');
     $all_clients_id = array();
     $all_lists = array();
     if (!function_exists('curl_init')) {
         return __('curl_init is not defined ', 'rapidology');
     }
     // Get cURL resource
     $curl = curl_init();
     // Set some options
     curl_setopt_array($curl, array(CURLOPT_RETURNTRANSFER => 1, CURLOPT_URL => $request_url, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_USERPWD => $api_key . ':x'));
     // Send the request & save response to $resp
     $resp = curl_exec($curl);
     $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
     // Close request to clear up some resources
     curl_close($curl);
     $clients_array = json_decode($resp, true);
     if ('200' == $httpCode) {
         $error_message = 'success';
         foreach ($clients_array as $client => $client_details) {
             $all_clients_id[] = $client_details['ClientID'];
         }
         if (!empty($all_clients_id)) {
             foreach ($all_clients_id as $client) {
                 $wrap = new CS_REST_Clients($client, $auth);
                 $lists_data = $wrap->get_lists();
                 foreach ($lists_data->response as $list => $single_list) {
                     $all_lists[$single_list->ListID]['name'] = $single_list->Name;
                     $wrap_stats = new CS_REST_Lists($single_list->ListID, $auth);
                     $result_stats = $wrap_stats->get_stats();
                     $all_lists[$single_list->ListID]['subscribers_count'] = sanitize_text_field($result_stats->response->TotalActiveSubscribers);
                     $all_lists[$single_list->ListID]['growth_week'] = sanitize_text_field($this->calculate_growth_rate('campaign_monitor_' . $single_list->ListID));
                 }
             }
         }
         $this->update_account('campaign_monitor', sanitize_text_field($name), array('api_key' => sanitize_text_field($api_key), 'lists' => $all_lists, 'is_authorized' => 'true'));
     } else {
         if ('401' == $httpCode) {
             $error_message = __('invalid API key', 'rapidology');
         } else {
             $error_message = $httpCode;
         }
     }
     return $error_message;
 }
Esempio n. 2
0
<?php

require_once '../../csrest_lists.php';
$auth = array('access_token' => 'your access token', 'refresh_token' => 'your refresh token');
$wrap = new CS_REST_Lists('List ID', $auth);
$result = $wrap->get_stats();
echo "Result of GET /api/v3.1/lists/{ID}/stats\n<br />";
if ($result->was_successful()) {
    echo "Got list stats\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>';