/** * 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; }
/** * Get fully configured and authenticated Twitter API client. * @return TwitterApiClient */ public static function api_client($id = null) { gltw_log(); if (!isset($clients[$id])) { $clients[$id] = TwitterApiClient::create_instance(is_null($id)); } return $clients[$id]; }
/** * Get fully configured and authenticated Twitter API client. * @return TwitterApiClient */ function twitter_api_client($id = null) { static $clients = array(); if (!isset($clients[$id])) { twitter_api_include('core'); $clients[$id] = TwitterApiClient::create_instance(is_null($id)); } return $clients[$id]; }
/** * Get client instance authenticated with 'system' credentials * @param bool whether we're getting the system default client which is expected to be authed * @return TwitterApiClient */ public static function create_instance($default = true) { $Client = new TwitterApiClient(); extract(_twitter_api_config()); if ($default) { if (!$consumer_key || !$consumer_secret || !$access_key || !$access_secret) { trigger_error(__('Twitter application not fully configured', 'twitter-api')); } $Client->set_oauth($consumer_key, $consumer_secret, $access_key, $access_secret); } else { if ($consumer_key && $consumer_secret) { $Client->set_oauth($consumer_key, $consumer_secret); } } return $Client; }
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; } } }
<?php require_once dirname(__FILE__) . '/../../vendor/lime/lime.php'; require_once dirname(__FILE__) . '/lib/MockTwitterApiServer.class.php'; require_once dirname(__FILE__) . '/../../lib/TwitterApiClient.class.php'; $t = new lime_test(46, new lime_output_color()); $client = new TwitterApiClient(new MockTwitterApiServer()); // blockUser() // createFavorite() // createFriendship() // deleteDirectMessage() // deleteFavorite() // deleteFriendship() // deleteStatus() // existsFriendship() $t->diag('existsFriendship()'); $t->is($client->existsFriendship('a', 'b'), true, 'existsFriendship() retrieves friendship status'); // followUser() // getDirectMessage() $t->diag('getDirectMessage()'); $dms = $client->getDirectMessage(155216447); $t->isa_ok($dms, 'TwitterDirectMessage', 'getDirectMessage() retrieves a TwitterDirectMessage'); $t->is($dms->sender->screen_name, 'duboisnicolas', 'getDirectMessage() retrieves correctly dm sender'); $t->is($dms->recipient->screen_name, 'n1k0', 'getDirectMessage() retrieves correctly dm recipient'); // getDirectMessages() $t->diag('getDirectMessages()'); $dms = $client->getDirectMessages(); $t->isa_ok($dms, 'TwitterDirectMessageCollection', 'getDirectMessages() retrieves a TwitterDirectMessageCollection'); $t->is($dms[0]->sender->screen_name, 'duboisnicolas', 'getDirectMessages() retrieves correctly dm sender'); $t->is($dms[0]->recipient->screen_name, 'n1k0', 'getDirectMessages() retrieves correctly dm recipient'); // getDowntimeSchedule()
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; } }
<?php /** * This command line wizard walks through the app authorization via the * command line and returns the access keys needed to start interacting with the API. * * To execute, run "php authorize.php" form the command line. **/ require __DIR__ . '/twitter-client.php'; $Client = new TwitterApiClient(); $config = array(); echo "\nThis command line wizard walks through the app authorization via the command line and returns the access keys needed to start interacting with the API.\n\n"; echo "Enter your consumer key: "; $config['consumer_key'] = trim(fgets(STDIN)); echo "\n\nEnter your consumer secret: "; $config['consumer_secret'] = trim(fgets(STDIN)); // Require client library and authorize an instance with just application details $Client->set_oauth($config['consumer_key'], $config['consumer_secret']); // Ask twitter for a request token and specify a callback parameter (for desktop we use "oob" to get a PIN) try { $Token = $Client->get_oauth_request_token('oob'); $redirect = $Token->get_authorization_url(); } catch (TwitterApiException $Ex) { 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);