/** * Calls the Search/tweets method of the Twitter API for particular Twitter Search Parameters and returns the list of tweets that match the search criteria. * * @param mixed $twitterSearchParams The Twitter Search Parameters that are passed to Twitter API. Read more here https://dev.twitter.com/docs/api/1.1/get/search/tweets * * @return array $tweets */ protected function getTweets($twitterSearchParams) { $Client = new TwitterApiClient(); //Use the TwitterAPIClient $Client->set_oauth($this->consumer_key, $this->consumer_secret, $this->access_key, $this->access_secret); $tweets = $Client->call('search/tweets', $twitterSearchParams, 'GET'); //call the service and get the list of tweets unset($Client); return $tweets; }
function blu_get_twitter_feed($user_id) { // Cache the results if they haven't been cached delete_transient('blu_get_twitter_feed_' . $user_id); if (($cache = get_transient('blu_get_twitter_feed_' . $user_id)) === false) { if (!of_get_option('twitter_api_key')) { $return_array['twitter'] = 'Configure your Twitter API key in Theme Options'; } else { if (!class_exists('TwitterApiClient')) { require_once 'inc/twitter-api.php'; } $Client = new TwitterApiClient(); $Client->set_oauth(of_get_option('twitter_api_key'), of_get_option('twitter_api_secret'), of_get_option('twitter_access_token'), of_get_option('twitter_access_token_secret')); try { $args = array('screen_name' => $user_id); $bl_data['user'] = @$Client->call('users/show', $args, 'GET'); $bl_data['tweets'] = @$Client->call('statuses/user_timeline', array('screen_name' => $user_id, 'count' => 4), 'GET'); set_transient('blu_get_twitter_feed_' . $user_id, $bl_data, 3600); return $bl_data; } catch (TwitterApiException $Ex) { return $Ex; } } } else { return $cache; } }
public function get_social_counter($service_id, $user_id, $disable_cache = false) { $buffy_array = 0; //use in g+ $expl_maches = ''; if ($this->in_cache($service_id, $user_id) === false or $disable_cache === true) { //disable cache here try { switch ($service_id) { case 'facebook': $td_data = $this->get_url("http://facebook.com/{$user_id}"); if ($td_data === false) { $this->log(__FUNCTION__, 'The get_url method FAILED, returning 0'); $buffy_array = 0; break; } $pattern = "/id=\"PagesLikesCountDOMID\">(.*?)<\\/span><\\/span>/i"; //first match - get the string with number of likes and the `likes` word preg_match_all($pattern, $td_data, $matches); //replace all the classes in the sting returned by first regular expression $sub_string = preg_replace('/class=\\"(.*?)\\"/i', '', $matches[1][0]); //extract only the numbers $buffy_array = $this->extract_numbers_from_string($sub_string); break; case 'twitter': $twitter_worked = false; //check 1 via https $td_data = $this->get_url("https://twitter.com/{$user_id}"); if ($td_data === false) { $this->log(__FUNCTION__, 'The get_url method FAILED, we are trying again via the api'); } else { $pattern = "/title=\"(.*)\"(.*)data-nav=\"followers\"/i"; preg_match_all($pattern, $td_data, $matches); if (!empty($matches[1][0])) { //$td_buffer_counter_fix = str_replace(array('.', ',', ' ', ' '), '', htmlentities($matches[1][0])); //old radu hack to filter numbers 2/2/2015 $td_buffer_counter_fix = $this->extract_numbers_from_string(htmlentities($matches[1][0])); // $td_buffer_counter_fix = str_replace(',','', $matches[1][0]); $buffy_array = (int) $td_buffer_counter_fix; if (!empty($buffy_array) and is_numeric($buffy_array)) { $twitter_worked = true; //skip twitter second check it worked! } } } //check 2 via twitter api client - we only get here if the first check did not returned anything if ($twitter_worked === false) { if (!class_exists('TwitterApiClient')) { require_once 'twitter-client.php'; $Client = new TwitterApiClient(); $Client->set_oauth(YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET, SOME_ACCESS_KEY, SOME_ACCESS_SECRET); try { $path = 'users/show'; $args = array('screen_name' => $user_id); $data = @$Client->call($path, $args, 'GET'); if (!empty($data['followers_count'])) { $buffy_array = (int) $data['followers_count']; //set the buffer } } catch (TwitterApiException $Ex) { //twitter rate limit will show here //print_r($Ex); } } } break; case 'vimeo': //$td_data = @$this->get_json("http://vimeo.com/api/v2/channel/$user_id/info.json"); //if (!empty($td_data['total_subscribers'])) { // $buffy_array = (int) $td_data['total_subscribers']; //} else { $td_data = @$this->get_url("http://vimeo.com/{$user_id}"); $pattern = "/<b class=\"stat_list_count\">(.*?)<\\/b>(\\s+)<span class=\"stat_list_label\">likes<\\/span>/"; preg_match($pattern, $td_data, $matches); if (!empty($matches[1])) { $buffy_array = (int) $matches[1]; } //} break; case 'youtube': // // channel string, if it's present it is not necessary for getting data // $user_id = str_replace("channel/", "", $user_id); // // $td_data = @$this->get_json("http://gdata.youtube.com/feeds/api/users/$user_id?alt=json"); // if (!empty($td_data['entry']['yt$statistics']['subscriberCount'])) { // $buffy_array = (int) $td_data['entry']['yt$statistics']['subscriberCount']; // } $url = "https://www.googleapis.com/youtube/v3/channels?part=statistics&key=AIzaSyBneuqXGHEXQiJlWUOv23_FA4CzpsHaS6I"; $search_id = str_replace("channel/", "", $user_id); if (strpos($user_id, "channel/") === 0) { $url .= "&id={$search_id}"; } else { $url .= "&forUsername={$search_id}"; } $td_data = @$this->get_json($url); $subscriberCount = @$td_data['items'][0]['statistics']['subscriberCount']; if (!empty($subscriberCount)) { $buffy_array = (int) $subscriberCount; } break; case 'googleplus': $td_data = @$this->get_json("https://www.googleapis.com/plus/v1/people/{$user_id}?key=AIzaSyA1hsdPPNpkS3lvjohwLNkOnhgsJ9YCZWw"); if (!empty($td_data['plusOneCount'])) { $buffy_array = (int) $td_data['plusOneCount']; } else { $td_data = @$this->get_url("https://plus.google.com/{$user_id}/posts"); $pattern = "/<span role=\"button\" class=\"d-s o5a\" tabindex=\"0\">(.*?)<\\/span>/"; preg_match($pattern, $td_data, $matches); if (!empty($matches[1])) { $expl_maches = explode(' ', trim($matches[1])); $buffy_array = str_replace(array('.', ','), array(''), $expl_maches[0]); } } break; case 'instagram': $td_data = @$this->get_url("http://instagram.com/{$user_id}#"); //$pattern = "/followed_by\":(.*?),\"follows\":/"; $pattern = "/followed_by\"\\:\\{\"count\"\\:(.*?)\\}\\,\"/"; preg_match($pattern, $td_data, $matches); if (!empty($matches[1])) { $buffy_array = (int) $matches[1]; } break; case 'soundcloud': $td_data = @$this->get_json("http://api.soundcloud.com/users/{$user_id}.json?client_id=97220fb34ad034b5d4b59b967fd1717e"); if (!empty($td_data['followers_count'])) { $buffy_array = (int) $td_data['followers_count']; } break; case 'rss': $buffy_array = (int) $user_id; break; } // if the cache is disable, do not run the cache things if ($disable_cache === true) { return $buffy_array; } //case 1; we have response from service; write the cache arrya with new values if ($buffy_array > 0) { $local_cash['count'] = $buffy_array; if ($buffy_array > 0) { $local_cash['ok_count'] = $buffy_array; } $local_cash['timestamp'] = time(); $local_cash['expires'] = 10800; $local_cash['uid'] = $user_id; //set to true the flag to save $this->do_transient_save = true; //send the service array to save the cache $this->save_cache($service_id, $local_cash); /* * case 2; we DON'T have response from service, then get the cache array for this service, * and rewrite the array in cache, with default values */ } else { $local_cash = $this->get_cache($service_id, $user_id); if (is_array($local_cash) and isset($local_cash['ok_count']) > 0) { $buffy_array = intval($local_cash['ok_count']); } else { $buffy_array = 0; } $local_cash['timestamp'] = time(); $local_cash['count'] = 0; $local_cash['uid'] = $user_id; $local_cash['expires'] = 10800; //set to true the flag to save $this->do_transient_save = true; //send the service array to save the cache $this->save_cache($service_id, $local_cash); } } catch (Exception $e) { } return $buffy_array; //case 3, the cache is true; time < expire } else { $local_cash = $this->get_cache($service_id, $user_id); if (is_array($local_cash) and array_key_exists('ok_count', $local_cash)) { $this->log(__FUNCTION__, "{$service_id} - {$user_id} found in cache (ok_count): " . intval($local_cash['ok_count'])); return intval($local_cash['ok_count']); } else { $this->log(__FUNCTION__, "{$service_id} - {$user_id} found in cache but the cache is empty or something"); return 0; } } }
echo 'Status ', $Ex->getStatus(), '. Error ' . $Ex->getCode(), ' - ', $Ex->getMessage(), "\n"; exit; } // Redirect your authenticating user to get a verifier echo "\n\n", "Authorize the token:\n", ' > ', $redirect, "\n", "\n", "Then enter your verifier: "; $some_verifier = trim(fgets(STDIN)); echo "\n"; $Client->set_oauth($config['consumer_key'], $config['consumer_secret'], $Token->key, $Token->secret); // Ask twitter for a request token and specify a callback parameter (for desktop app we use "oob" to get a pin number) try { $Token = $Client->get_oauth_access_token($some_verifier); } catch (TwitterApiException $Ex) { echo 'Status ', $Ex->getStatus(), '. Error ' . $Ex->getCode(), ' - ', $Ex->getMessage(), "\n"; exit; } $Client->set_oauth($config['consumer_key'], $config['consumer_secret'], $Token->key, $Token->secret); $config['access_key'] = $Token->key; $config['access_secret'] = $Token->secret; // Now you're ready to make authorized API calls try { $path = 'account/verify_credentials'; $args = array('skip_status' => true); $data = $Client->call($path, $args, 'GET'); echo 'Authenticated as @', $data['screen_name'], ' #', $data['id_str'], "\n\n"; } catch (TwitterApiException $Ex) { echo 'Status ', $Ex->getStatus(), '. Error ' . $Ex->getCode(), ' - ', $Ex->getMessage(), "\n"; exit; } echo '<?php $config = ', var_export($config, 1), '; ';