/** * Share Counts API Connectivity check * */ public static function share_counts_api_connectivity_check() { // if we already checked and it is successful, then do not call the API again $share_counts_connect_check = self::get_option('share_counts_connect_check'); if (isset($share_counts_connect_check) && $share_counts_connect_check == 'SUCCESS') { return $share_counts_connect_check; } $services_config = ShareaholicSeqShareCount::get_services_config(); $services = array_keys($services_config); $param_string = implode('&services[]=', $services); $share_counts_api_url = url('shareaholic/api/share_counts/v1', array('absolute' => TRUE)) . '?action=shareaholic_share_counts_api&url=https%3A%2F%2Fwww.google.com%2F&services[]=' . $param_string; $cache_key = 'share_counts_api_connectivity_check'; // fetch cached response if it exists or has not expired $response = ShareaholicCache::get($cache_key); if (!$response) { $response = ShareaholicHttp::send($share_counts_api_url, array('method' => 'GET'), true); } $response_status = self::get_share_counts_api_status($response); // if this was the first time we are doing this and it failed, disable // the share counts API if (empty($share_counts_connect_check) && $response_status == 'FAIL') { self::update_options(array('disable_internal_share_counts_api' => 'on')); } if ($response_status == 'SUCCESS') { ShareaholicCache::set($cache_key, $response, SHARE_COUNTS_CHECK_CACHE_LENGTH); } self::update_options(array('share_counts_connect_check' => $response_status)); return $response_status; }
/** * Function to handle the share count API requests * */ public static function share_counts_api() { // sometimes the drupal http request function throws errors so setting handler set_error_handler(array('ShareaholicPublic', 'custom_error_handler')); $cache_key = 'shr_api_res-' . md5($_SERVER['QUERY_STRING']); $result = ShareaholicCache::get($cache_key); if (!$result) { $url = isset($_GET['url']) ? $_GET['url'] : NULL; $services = isset($_GET['services']) ? $_GET['services'] : NULL; $result = array(); if (is_array($services) && count($services) > 0 && !empty($url)) { if (self::has_curl()) { $shares = new ShareaholicCurlMultiShareCount($url, $services); } else { $shares = new ShareaholicSeqShareCount($url, $services); } $result = $shares->get_counts(); if (isset($result['data'])) { ShareaholicCache::set($cache_key, $result, SHARE_COUNTS_CHECK_CACHE_LENGTH); } } } header('Content-Type: application/json'); echo json_encode($result); exit; }
/** * Function to handle the share count API requests * */ public static function share_counts_api() { // sometimes the drupal http request function throws errors so setting handler set_error_handler(array('ShareaholicPublic', 'custom_error_handler')); $debug_mode = isset($_GET['debug']) && $_GET['debug'] === '1'; $cache_key = 'shr_api_res-' . md5($_SERVER['QUERY_STRING']); $result = ShareaholicCache::get($cache_key); $has_curl_multi = self::has_curl(); if (!$result) { $url = isset($_GET['url']) ? $_GET['url'] : NULL; $services = isset($_GET['services']) ? $_GET['services'] : NULL; $result = array(); $options = array(); if ($debug_mode && isset($_GET['timeout'])) { $options['timeout'] = intval($_GET['timeout']); } if (is_array($services) && count($services) > 0 && !empty($url)) { if ($debug_mode && isset($_GET['client'])) { if ($has_curl_multi && $_GET['client'] !== 'seq') { $shares = new ShareaholicCurlMultiShareCount($url, $services, $options); } else { $shares = new ShareaholicSeqShareCount($url, $services, $options); } } else { if ($has_curl_multi) { $shares = new ShareaholicCurlMultiShareCount($url, $services, $options); } else { $shares = new ShareaholicSeqShareCount($url, $services, $options); } } $result = $shares->get_counts(); if ($debug_mode) { $result['has_curl_multi'] = $has_curl_multi; $result['curl_type'] = get_class($shares); $result['raw'] = $shares->raw_response; } if (isset($result['data']) && !$debug_mode) { ShareaholicCache::set($cache_key, $result, SHARE_COUNTS_CHECK_CACHE_LENGTH); } } } header('Content-Type: application/json'); echo json_encode($result); exit; }