/** * This test may fail if the APIs fail */ public function testMissingServices() { // test that this function returns response WITHOUT facebook $share_count = new ShareaholicSeqShareCount('https://dev.losloslos.com/', $this->services, $this->options); $response = $share_count->get_counts(); $this->assertNotNull($response, 'The response array should not be null'); $this->assertNull($response['data']['facebook'], 'The facebook count should be null'); }
/** * 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() { $debug_mode = isset($_GET['debug']) && $_GET['debug'] === '1'; $url = isset($_GET['url']) ? $_GET['url'] : ''; $services = isset($_GET['services']) ? $_GET['services'] : array(); $services = self::parse_services($services); $cache_key = 'shr_api_res-' . md5($url); if (empty($url) || empty($services)) { $result = array(); } else { $result = get_transient($cache_key); } $has_curl_multi = self::has_curl(); if (!$result || $debug_mode || !self::has_services_in_result($result, $services)) { if (isset($result['services']) && !$debug_mode) { $services = array_keys(array_flip(array_merge($result['services'], $services))); } $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) { $result['services'] = $services; set_transient($cache_key, $result, SHARE_COUNTS_CHECK_CACHE_LENGTH); } } } header('Content-Type: application/json'); header('Cache-Control: max-age=180'); // 3 minutes 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; }