Created
October 29, 2012 12:40
-
-
Save agustinhaller/3973298 to your computer and use it in GitHub Desktop.
twitter class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /****** STYLE GUIDES *****/ | |
| /* About parameters: */ | |
| /* | |
| To make it flexible, all parameters have to be optional (that's why it receives an array) but there are some that are hard to remember and could | |
| be the default for certain scenarios. So if the parameter needed is in the array $parameter use it, if not use | |
| the default fallback. | |
| */ | |
| class Twitter{ | |
| private $tmhOAuth; | |
| private $authenticated_user; | |
| // This function will receive an array with users, it will iterate that array and try | |
| // to init tmhOAuth | |
| public function __construct($CONSUMER_KEY, $CONSUMER_SECRET, $USER_TOKEN, $USER_SECRET) | |
| { | |
| $this->tmhOAuth = new tmhOAuth(array( | |
| 'consumer_key' => $CONSUMER_KEY, | |
| 'consumer_secret' => $CONSUMER_SECRET, | |
| 'user_token' => $USER_TOKEN, | |
| 'user_secret' => $USER_SECRET | |
| )); | |
| } | |
| public function getRateLimits() | |
| { | |
| $resources_rate_limits = array(); | |
| $tmhOAuth = $this->tmhOAuth; | |
| $tmhOAuth->request('GET', $tmhOAuth->url('1.1/application/rate_limit_status'), array( | |
| 'resources' => "friendships,users,followers,search,statuses,friends" | |
| )); | |
| if($tmhOAuth->response['code'] == 200) | |
| { | |
| $data = json_decode($tmhOAuth->response['response'], true); | |
| $resources_rate_limits = $data['resources']; | |
| } | |
| return $resources_rate_limits; | |
| } | |
| // This function checks if the specified tweet still exists or not | |
| public function tweetExists($tweet_id) | |
| { | |
| $tweet = array( "exists"=>false, | |
| "object"=>null); | |
| $tmhOAuth = $this->tmhOAuth; | |
| $tmhOAuth->request('GET', $tmhOAuth->url('1.1/statuses/show'), array( | |
| 'id' => $tweet_id | |
| )); | |
| $result_aux = "NOT RUN"; | |
| if($tmhOAuth->response['code'] == 200) | |
| { | |
| $data = json_decode($tmhOAuth->response['response'], true); | |
| //var_dump($data); | |
| // Tweet object bridge | |
| $tweet_bridge_data = $data; | |
| $tweet_created_date_aux = strtotime($tweet_bridge_data['created_at']); | |
| $tweet_created_date = date("Y-m-d H:i:s", $tweet_created_date_aux); | |
| $tweet_user_id = $tweet_bridge_data['user']['id_str']; | |
| $tweet_bridge = array( | |
| "tweet_id" => $tweet_bridge_data['id_str'], | |
| "tweet_text" => $tweet_bridge_data['text'], | |
| "tweet_created_date" => $tweet_created_date, | |
| "tweet_user_id" => $tweet_user_id, | |
| "tweet_user_username" => $tweet_bridge_data['user']['screen_name'], | |
| "tweet_user_name" => $tweet_bridge_data['user']['name'], | |
| "tweet_user_profile_url" => "http://twitter.com/".$tweet_bridge_data['user']['screen_name'], | |
| "tweet_user_profile_image_url" => $tweet_bridge_data['user']['profile_image_url'] | |
| ); | |
| $tweet = array( "exists"=>true, | |
| "object"=>$tweet_bridge); | |
| $result_aux = "OK"; | |
| } | |
| else// Twitter call error | |
| { | |
| $tw_response = $tmhOAuth->response['response']; | |
| $tweet = array( "exists"=>false, | |
| "object"=>null, | |
| "tw_response"=>$tw_response); | |
| $result_aux = "ERROR: ".$tw_response; | |
| } | |
| // Get rate limits | |
| $resources_rate_limits = $this->getRateLimits(); | |
| // print_str($resources_rate_limits); | |
| $twitterManager = TwitterManager::getManager(); | |
| $twitterManager->newCall($this->authenticated_user, "/statuses/show/:id", $result_aux, $resources_rate_limits); | |
| return $tweet; | |
| } | |
| public function getAuthenticatedUser() | |
| { | |
| return $this->authenticated_user; | |
| } | |
| public function setAuthenticatedUser($username) | |
| { | |
| $this->authenticated_user = $username; | |
| } | |
| // This function retrives all the results for certain search | |
| public function getTwitterSearch($search_query, $since_id=null) | |
| { | |
| $tmhOAuth = $this->tmhOAuth; | |
| // Now perform the search | |
| $last_page = 1; | |
| $keep_searching = true; | |
| $total_results_count = 0; | |
| $total_results = array(); | |
| $max_id = null; | |
| $processed_tweets_ids = array(); | |
| while($last_page <= 20 && $keep_searching) | |
| { | |
| $search_args = array(); | |
| $search_args['q'] = $search_query; | |
| $search_args['count'] = 100; | |
| if($since_id!=null) | |
| { | |
| $search_args['since_id'] = $since_id; | |
| } | |
| if($max_id!=null) | |
| { | |
| $search_args['max_id'] = $max_id; | |
| } | |
| print_str($search_args, "search_args"); | |
| $result_aux = "NOT RUN"; | |
| $tmhOAuth->request('GET', $tmhOAuth->url('1.1/search/tweets'), $search_args); | |
| if($tmhOAuth->response['code'] == 200) | |
| { | |
| $data = json_decode($tmhOAuth->response['response'], true); | |
| $result_aux = "OK"; | |
| $page_results = array(); | |
| $page_results_aux = $data['statuses']; | |
| $page_results_count = count($page_results_aux); | |
| print_str($page_results_count, "page_results_count"); | |
| if($page_results_count>0) | |
| { | |
| $total_results_count += $page_results_count; | |
| foreach($page_results_aux as $tweet) | |
| { | |
| $already_processed_tweet = in_array($tweet['id_str'], $processed_tweets_ids); | |
| if(!$already_processed_tweet) | |
| { | |
| $processed_tweets_ids[] = $tweet['id_str']; | |
| $page_results[] = $tweet; | |
| } | |
| } | |
| // Merge page results with total results | |
| $total_results = array_merge($total_results, $page_results); | |
| $last_page++; | |
| $keep_searching = true; | |
| $max_id = $page_results_aux[$page_results_count-1]['id_str']; | |
| } | |
| else | |
| { | |
| $keep_searching = false; | |
| } | |
| } | |
| else// Twitter call error | |
| { | |
| $keep_searching = false; | |
| $tw_response = $tmhOAuth->response['response']; | |
| $result_aux = "ERROR: ".$tw_response; | |
| } | |
| // Get rate limits | |
| $resources_rate_limits = $this->getRateLimits(); | |
| // print_str($resources_rate_limits); | |
| $twitterManager = TwitterManager::getManager(); | |
| $twitterManager->newCall($this->authenticated_user, "/search/tweets", $result_aux, $resources_rate_limits); | |
| }// END WHILE | |
| // Now we have the complete search space in $total_results, lets return it | |
| return $total_results; | |
| } | |
| // This function retrives the 100 latest retweets of certain tweet | |
| public function getRetweets($tweet_id) | |
| { | |
| $tmhOAuth = $this->tmhOAuth; | |
| $total_results = array(); | |
| $result_aux = "NOT RUN"; | |
| $tmhOAuth->request('GET', $tmhOAuth->url('1.1/statuses/retweets/'.$tweet_id), array( | |
| 'count' => 100 | |
| )); | |
| if($tmhOAuth->response['code'] == 200) | |
| { | |
| $data = json_decode($tmhOAuth->response['response'], true); | |
| $result_aux = "OK"; | |
| $page_results_count = count($data); | |
| // print_str($data, "data"); | |
| print_str($page_results_count, "page_results_count"); | |
| if($page_results_count>0) | |
| { | |
| $total_results = $data; | |
| // print_str($total_results, "total_results"); | |
| } | |
| } | |
| else// Twitter call error | |
| { | |
| $tw_response = $tmhOAuth->response['response']; | |
| $result_aux = "ERROR: ".$tw_response; | |
| } | |
| // Get rate limits | |
| $resources_rate_limits = $this->getRateLimits(); | |
| // print_str($resources_rate_limits); | |
| $twitterManager = TwitterManager::getManager(); | |
| $twitterManager->newCall($this->authenticated_user, "/statuses/retweets/:id", $result_aux, $resources_rate_limits); | |
| return $total_results; | |
| } | |
| // NEEDS TESTING | |
| // This function returns an array with the ids of the user followers | |
| public function getTwitterFollowers($twitter_username, $cursor=null) | |
| { | |
| $twitter_followers = array(); | |
| $tmhOAuth = $this->tmhOAuth; | |
| if($cursor==null) | |
| { | |
| $cursor = -1; | |
| } | |
| $request_qty = 0; | |
| $last_cursor = ''; | |
| //12 is a fair amount (15 is the max!) of requests that we can do every 15 minutes to this endpoint | |
| //The real amount is 15 but we must reserve 1 request for the followers update method (just consider it can have an active contest!) | |
| while($cursor != 0 && $request_qty<12) | |
| { | |
| $result_aux = "NOT RUN"; | |
| $tmhOAuth->request('GET', $tmhOAuth->url('1.1/followers/ids'), array( | |
| 'stringify_ids' => true, | |
| 'screen_name' => $twitter_username, | |
| 'cursor' => $cursor | |
| )); | |
| $request_qty++; | |
| if($tmhOAuth->response['code'] == 200) | |
| { | |
| $data = json_decode($tmhOAuth->response['response'], true); | |
| $data_ids = $data['ids']; | |
| $result_aux = "OK"; | |
| if($data_ids!=null) | |
| { | |
| $count_aux = count($data['ids']); | |
| $twitter_followers = array_merge($twitter_followers, $data['ids']); | |
| $cursor = $data['next_cursor_str']; | |
| if(isset($data['next_cursor_str']) && $data['next_cursor_str']!="") | |
| { | |
| $last_cursor = $data['next_cursor_str']; | |
| print_str($last_cursor, "LAST CURSOR"); | |
| } | |
| // $cursor = $data['next_cursor_str']; | |
| /***** WE SHOULD STORE THIS CURSOR IN THE DATABASE IN ORDER TO CONTINUE THE UPDATE OF FOLLOWERS ****/ | |
| // saveCursor($cursor,$twitter_username); | |
| // print_str($data, "DATA"); | |
| // print_str($cursor, "CURSOR"); | |
| print_str($count_aux, "count_aux"); | |
| } | |
| else | |
| { | |
| print_str($tmhOAuth->response['response'], "TWITTER ERROR for user: ".$twitter_username); | |
| } | |
| } | |
| else// Twitter call error | |
| { | |
| $tw_response = $tmhOAuth->response['response']; | |
| $result_aux = "ERROR: ".$tw_response; | |
| print_str($tmhOAuth->response['response'], "TWITTER ERROR for user: ".$twitter_username); | |
| break; | |
| } | |
| // Get rate limits | |
| $resources_rate_limits = $this->getRateLimits(); | |
| // print_str($resources_rate_limits); | |
| $twitterManager = TwitterManager::getManager(); | |
| $twitterManager->newCall($this->authenticated_user, "/followers/ids", $result_aux, $resources_rate_limits); | |
| } | |
| $array_ret = array( | |
| "twitter_followers" => $twitter_followers, | |
| "last_cursor" => $last_cursor | |
| ); | |
| return $array_ret; | |
| } | |
| // NEEDS TESTING | |
| // This function returns an array with the ids of the user latest 5000 followers | |
| public function getLatestFollowers($twitter_username) | |
| { | |
| $latest_followers = array(); | |
| $tmhOAuth = $this->tmhOAuth; | |
| $result_aux = "NOT RUN"; | |
| $tmhOAuth->request('GET', $tmhOAuth->url('1.1/followers/ids'), array( | |
| 'stringify_ids' => true, | |
| 'screen_name' => $twitter_username | |
| )); | |
| if($tmhOAuth->response['code'] == 200) | |
| { | |
| $data = json_decode($tmhOAuth->response['response'], true); | |
| $data_ids = $data['ids']; | |
| $result_aux = "OK"; | |
| if($data_ids!=null) | |
| { | |
| $latest_followers = $data_ids; | |
| } | |
| else | |
| { | |
| print_str($tmhOAuth->response['response'], "TWITTER ERROR for user: ".$twitter_username); | |
| } | |
| } | |
| else// Twitter call error | |
| { | |
| $tw_response = $tmhOAuth->response['response']; | |
| $result_aux = "ERROR: ".$tw_response; | |
| print_str($tmhOAuth->response['response'], "TWITTER ERROR for user: ".$twitter_username); | |
| } | |
| // Get rate limits | |
| $resources_rate_limits = $this->getRateLimits(); | |
| // print_str($resources_rate_limits); | |
| $twitterManager = TwitterManager::getManager(); | |
| $twitterManager->newCall($this->authenticated_user, "/followers/ids", $result_aux, $resources_rate_limits); | |
| return $latest_followers; | |
| } | |
| // This function returns twitter user data | |
| public function getUserData($twitter_username) | |
| { | |
| $user_data = null; | |
| $tmhOAuth = $this->tmhOAuth; | |
| $result_aux = "NOT RUN"; | |
| $tmhOAuth->request('GET', $tmhOAuth->url('1.1/users/lookup'), array( | |
| 'screen_name' => $twitter_username | |
| )); | |
| if($tmhOAuth->response['code'] == 200) | |
| { | |
| $data = json_decode($tmhOAuth->response['response'], true); | |
| $result_aux = "OK"; | |
| // Reduce data to anly needed one | |
| $twitter_user = $data[0]; | |
| if(isset($twitter_user) && $twitter_user!=null && $twitter_user!="") | |
| { | |
| $account_created_date_aux = strtotime($twitter_user['created_at']); | |
| $account_created_date = date("Y-m-d H:i:s", $account_created_date_aux); | |
| $user_data = array( | |
| "id_str" => $twitter_user['id_str'], | |
| "screen_name" => $twitter_user['screen_name'], | |
| "name" => utf8_decode($twitter_user['name']), | |
| "description" => utf8_decode($twitter_user['description']), | |
| "url" => $twitter_user['url'], | |
| "location" => $twitter_user['location'], | |
| "profile_image_url" => $twitter_user['profile_image_url'], | |
| "created_at" => $account_created_date, | |
| "statuses_count" => $twitter_user['statuses_count'], | |
| "favourites_count" => $twitter_user['favourites_count'], | |
| "friends_count" => $twitter_user['friends_count'], | |
| "followers_count" => $twitter_user['followers_count'] | |
| ); | |
| } | |
| else | |
| { | |
| print_str($tmhOAuth->response['response'], "TWITTER ERROR for user: ".$twitter_username); | |
| } | |
| } | |
| else// Twitter call error | |
| { | |
| $tw_response = $tmhOAuth->response['response']; | |
| $result_aux = "ERROR: ".$tw_response; | |
| print_str($tmhOAuth->response['response'], "TWITTER ERROR for user: ".$twitter_username); | |
| } | |
| // Get rate limits | |
| $resources_rate_limits = $this->getRateLimits(); | |
| // print_str($resources_rate_limits); | |
| $twitterManager = TwitterManager::getManager(); | |
| $twitterManager->newCall($this->authenticated_user, "/users/lookup", $result_aux, $resources_rate_limits); | |
| return $user_data; | |
| } | |
| // This functions goes to twitter and brings back all the info of up to 100 users | |
| function getBulkUsersData($tw_100_account_ids) | |
| { | |
| $twitter_users_data = null; | |
| $tmhOAuth = $this->tmhOAuth; | |
| $users_ids = implode(",", $tw_100_account_ids); | |
| $result_aux = "NOT RUN"; | |
| $tmhOAuth->request('GET', $tmhOAuth->url('1.1/users/lookup'), array( | |
| 'user_id' => $users_ids | |
| )); | |
| if($tmhOAuth->response['code'] == 200) | |
| { | |
| $data = json_decode($tmhOAuth->response['response'], true); | |
| $result_aux = "OK"; | |
| $twitter_users_data = array(); | |
| // Reduce data to only needed one | |
| foreach($data as $twitter_user) | |
| { | |
| $account_created_date_aux = strtotime($twitter_user['created_at']); | |
| $account_created_date = date("Y-m-d H:i:s", $account_created_date_aux); | |
| $user_data = array( | |
| "id_str" => $twitter_user['id_str'], | |
| "screen_name" => $twitter_user['screen_name'], | |
| "name" => utf8_decode($twitter_user['name']), | |
| "description" => utf8_decode($twitter_user['description']), | |
| "url" => $twitter_user['url'], | |
| "location" => $twitter_user['location'], | |
| "profile_image_url" => $twitter_user['profile_image_url'], | |
| "created_at" => $account_created_date, | |
| "statuses_count" => $twitter_user['statuses_count'], | |
| "favourites_count" => $twitter_user['favourites_count'], | |
| "friends_count" => $twitter_user['friends_count'], | |
| "followers_count" => $twitter_user['followers_count'] | |
| ); | |
| $twitter_users_data[] = $user_data; | |
| } | |
| } | |
| else// Twitter call error | |
| { | |
| $tw_response = $tmhOAuth->response['response']; | |
| $result_aux = "ERROR: ".$tw_response; | |
| } | |
| // Get rate limits | |
| $resources_rate_limits = $this->getRateLimits(); | |
| // print_str($resources_rate_limits); | |
| $twitterManager = TwitterManager::getManager(); | |
| $twitterManager->newCall($this->authenticated_user, "/users/lookup", $result_aux, $resources_rate_limits); | |
| return $twitter_users_data; | |
| } | |
| // This function returns twitter user data | |
| public function userFollowsBackUser($user_a, $user_b) | |
| { | |
| $user_follows_back_user = false; | |
| $tmhOAuth = $this->tmhOAuth; | |
| $args_aux = array(); | |
| // Check if user_a and user_b are ids or screen names | |
| if(is_numeric($user_a)) | |
| { | |
| $args_aux['source_id'] = $user_a; | |
| } | |
| else | |
| { | |
| $args_aux['source_screen_name'] = $user_a; | |
| } | |
| if(is_numeric($user_b)) | |
| { | |
| $args_aux['target_id'] = $user_b; | |
| } | |
| else | |
| { | |
| $args_aux['target_screen_name'] = $user_b; | |
| } | |
| $result_aux = "NOT RUN"; | |
| $tmhOAuth->request('GET', $tmhOAuth->url('1.1/friendships/show'), $args_aux); | |
| if($tmhOAuth->response['code'] == 200) | |
| { | |
| $data = json_decode($tmhOAuth->response['response'], true); | |
| $result_aux = "OK"; | |
| $target_user = $data['relationship']['target']; | |
| $source_user = $data['relationship']['source']; | |
| if($target_user['followed_by'] || $target_user['followed_by']=='true') | |
| { | |
| $user_follows_back_user = true; | |
| } | |
| } | |
| else// Error en twitter call | |
| { | |
| $tw_response = $tmhOAuth->response['response']; | |
| $result_aux = "ERROR: ".$tw_response; | |
| } | |
| // Get rate limits | |
| $resources_rate_limits = $this->getRateLimits(); | |
| // print_str($resources_rate_limits); | |
| $twitterManager = TwitterManager::getManager(); | |
| $twitterManager->newCall($this->authenticated_user, "/friendships/show", $result_aux, $resources_rate_limits); | |
| return $user_follows_back_user; | |
| } | |
| // NEEDS TESTING | |
| // This function returns an array with the ids of the user latest 5000 followers | |
| public function followUser($tw_username_to_follow) | |
| { | |
| $follow_ok = false; | |
| $tmhOAuth = $this->tmhOAuth; | |
| $result_aux = "NOT RUN"; | |
| $tmhOAuth->request('POST', $tmhOAuth->url('1.1/friendships/create'), array( | |
| 'screen_name' => $tw_username_to_follow | |
| )); | |
| if($tmhOAuth->response['code'] == 200) | |
| { | |
| $follow_ok = true; | |
| $result_aux = "OK"; | |
| } | |
| else// Twitter call error | |
| { | |
| $tw_response = $tmhOAuth->response['response']; | |
| $result_aux = "ERROR: ".$tw_response; | |
| } | |
| // Get rate limits | |
| $resources_rate_limits = $this->getRateLimits(); | |
| // print_str($resources_rate_limits); | |
| $twitterManager = TwitterManager::getManager(); | |
| $twitterManager->newCall($this->authenticated_user, "/friendships/create", $result_aux, $resources_rate_limits); | |
| return $follow_ok; | |
| } | |
| // NEEDS TESTING | |
| // This function returns an array with the ids of the user latest 5000 followers | |
| public function tweetText($text_to_tweet) | |
| { | |
| $response_array = null; | |
| $tmhOAuth = $this->tmhOAuth; | |
| $result_aux = "NOT RUN"; | |
| $tmhOAuth->request('POST', $tmhOAuth->url('1.1/statuses/update'), array( | |
| 'status' => utf8_encode($text_to_tweet), | |
| 'include_entities' => true | |
| )); | |
| if($tmhOAuth->response['code'] == 200) | |
| { | |
| $data = json_decode($tmhOAuth->response['response'], true); | |
| $object_result = array( | |
| "tweet_id" => $data['id_str'], | |
| "tweet_text" => $data['text'], | |
| "tweet_created_date" => $data['created_at'], | |
| "tweet_user_id" => $data['user']['id_str'], | |
| "tweet_user_username" => $data['user']['screen_name'], | |
| "tweet_user_name" => $data['user']['name'], | |
| "tweet_user_profile_image" => $data['user']['profile_image_url'] | |
| ); | |
| // Lets build the response array | |
| $response_array = array( | |
| 'tweet_ok' => true, | |
| 'tweet_id' => $data['id_str'], | |
| 'object_result' => $object_result | |
| ); | |
| $result_aux = "OK"; | |
| } | |
| else// Twitter call error | |
| { | |
| $tw_response = $tmhOAuth->response['response']; | |
| $result_aux = "ERROR: ".$tw_response; | |
| // Lets build the response array | |
| $response_array = array( | |
| 'tweet_ok' => false | |
| ); | |
| } | |
| // Get rate limits | |
| $resources_rate_limits = $this->getRateLimits(); | |
| // print_str($resources_rate_limits); | |
| $twitterManager = TwitterManager::getManager(); | |
| $twitterManager->newCall($this->authenticated_user, "/statuses/update", $result_aux, $resources_rate_limits); | |
| return $response_array; | |
| } | |
| // NEEDS TESTING | |
| // This function returns an array with the ids of the user latest 5000 followers | |
| public function retweetTweet($retweet_id) | |
| { | |
| $response_array = null; | |
| $tmhOAuth = $this->tmhOAuth; | |
| $result_aux = "NOT RUN"; | |
| $tmhOAuth->request('POST', $tmhOAuth->url('1.1/statuses/retweet/'.$retweet_id), array( | |
| 'include_entities' => true | |
| )); | |
| if($tmhOAuth->response['code'] == 200) | |
| { | |
| $data = json_decode($tmhOAuth->response['response'], true); | |
| $object_result = array( | |
| "tweet_id" => $data['id_str'], | |
| "tweet_text" => $data['text'], | |
| "tweet_created_date" => $data['created_at'], | |
| "tweet_user_id" => $data['user']['id_str'], | |
| "tweet_user_username" => $data['user']['screen_name'], | |
| "tweet_user_name" => $data['user']['name'], | |
| "tweet_user_profile_image" => $data['user']['profile_image_url'] | |
| ); | |
| // Lets build the response array | |
| $response_array = array( | |
| 'retweet_ok' => true, | |
| 'tweet_id' => $data['id_str'], | |
| 'object_result' => $object_result | |
| ); | |
| $result_aux = "OK"; | |
| } | |
| else// Twitter call error | |
| { | |
| $tw_response = $tmhOAuth->response['response']; | |
| $result_aux = "ERROR: ".$tw_response; | |
| // Lets build the response array | |
| $response_array = array( | |
| 'retweet_ok' => false | |
| ); | |
| } | |
| // Get rate limits | |
| $resources_rate_limits = $this->getRateLimits(); | |
| // print_str($resources_rate_limits); | |
| $twitterManager = TwitterManager::getManager(); | |
| $twitterManager->newCall($this->authenticated_user, "/statuses/retweet", $result_aux, $resources_rate_limits); | |
| return $response_array; | |
| } | |
| public function user_lookup($params = array()){ | |
| $this->tmhOAuth->request('GET', $this->tmhOAuth->url('1.1/users/lookup'), $params); | |
| if($this->tmhOAuth->response['code'] == 200) | |
| { | |
| $data = json_decode($this->tmhOAuth->response['response'], true); | |
| return $data; | |
| }else{ | |
| return false; | |
| } | |
| } | |
| public function tweet($params = array()){ | |
| /*** THIS ALLOW US TO PASS ONLY THE TEXT AS PARAMETER, OF AN ARRAY OF PARAMETERS TO BE MORE EXPLICIT ***/ | |
| if(!is_array($params)) $params = array("status"=>$params); | |
| $this->tmhOAuth->request('POST', $this->tmhOAuth->url('1.1/statuses/update'), $params); | |
| if($this->tmhOAuth->response['code'] == 200) | |
| { | |
| $data = json_decode($this->tmhOAuth->response['response'], true); | |
| return $data; | |
| }else{ | |
| return false; | |
| } | |
| } | |
| } | |
| // Class to manage Twitter calls flow | |
| // Instantiate it like this -> $pepe = TwitterManager::getManager(); | |
| Class TwitterManager | |
| { | |
| private static $manager; | |
| public $lookup_counter = 0; | |
| public $tweets_counter = 0; | |
| public $followers_counter = 0; | |
| public $friendships_counter = 0; | |
| public function __construct() | |
| { | |
| } | |
| // This is the singleton function | |
| public static function getManager() | |
| { | |
| if(!isset(self::$manager)) | |
| { | |
| self::$manager = new TwitterManager(); | |
| } | |
| return self::$manager; | |
| } | |
| // Manage API calls flow | |
| // This function adds 1 to $calls_counter and checks if it should trigger a sleep() or not | |
| public function newCall($tw_username, $endpoint, $result, $resources_rate_limits) | |
| { | |
| // First check if we have to trigger a sleep or not, we will only trigger sleeps for | |
| // "/users/lookup", "/search/tweets", "/followers/ids", "/friendships/show" | |
| $try_sleep = false; | |
| switch ($endpoint) | |
| { | |
| case '/users/lookup': | |
| $try_sleep = true; | |
| $this->lookup_counter++; | |
| $calls_counter_aux = $this->lookup_counter; | |
| $consecutive_calls = 5; | |
| break; | |
| case '/search/tweets': | |
| $try_sleep = true; | |
| $this->tweets_counter++; | |
| $calls_counter_aux = $this->tweets_counter; | |
| $consecutive_calls = 5; | |
| break; | |
| case '/followers/ids': | |
| $try_sleep = true; | |
| $this->followers_counter++; | |
| $calls_counter_aux = $this->followers_counter; | |
| $consecutive_calls = 5; | |
| break; | |
| case '/friendships/show': | |
| $try_sleep = true; | |
| $this->friendships_counter++; | |
| $calls_counter_aux = $this->friendships_counter; | |
| $consecutive_calls = 5; | |
| break; | |
| default: | |
| break; | |
| } | |
| if($try_sleep) | |
| { | |
| // Each five calls we trigger a sleep of 2 seconds | |
| if(($calls_counter_aux%$consecutive_calls)==0) | |
| { | |
| sleep(2); | |
| echo("<br/><br/>"); | |
| echo("sleeping"); | |
| echo("<br/><br/>"); | |
| } | |
| } | |
| // Now lets store this call in the db | |
| $possible_endpoints = array("/statuses/show/:id", "/search/tweets", "/statuses/retweets/:id", "/followers/ids", "/friendships/show", "/users/lookup", "/statuses/user_timeline/:id"); | |
| $endpoint_limits = array(); | |
| foreach ($resources_rate_limits as $resource_type) | |
| { | |
| foreach ($resource_type as $endpoint_name => $endpoint_values) | |
| { | |
| // $is_wanted_endpoint = in_array($endpoint_name, $possible_endpoints); | |
| // if($is_wanted_endpoint) | |
| // { | |
| // $endpoint_limits[] = array( | |
| // "endpoint" => $endpoint_name, | |
| // "remaining" => $endpoint_values['remaining'] | |
| // ); | |
| // } | |
| if($endpoint_name == $endpoint) | |
| { | |
| // Connect to db | |
| $conn = connect_db(); | |
| if($conn!=false) | |
| { | |
| // Then store data in db | |
| $q_irlt = " INSERT INTO twitter_rate_limits | |
| SET user = '".mysql_real_escape_string($tw_username)."', | |
| endpoint = '".mysql_real_escape_string($endpoint_name)."', | |
| date = '".date("Y-m-d H:i:s")."', | |
| result = '".mysql_real_escape_string($result)."', | |
| remaining = ".mysql_real_escape_string($endpoint_values['remaining']); | |
| $r_irlt = mysql_query($q_irlt); | |
| if($r_irlt) | |
| { | |
| // Insert OK | |
| print_str("Rate limits checked for endpoint '".$endpoint_name."' - OK"); | |
| } | |
| else// Error when saving to db | |
| { | |
| } | |
| } | |
| // Destroy db connection | |
| disconnect_db($conn); | |
| } | |
| }// END FOREACH 1 | |
| }// END FOREACH RESOURCES | |
| // Manage POST requests | |
| $post_endpoints = array("/friendships/create", "/statuses/update", "/statuses/retweet"); | |
| $is_limited_endpoint = in_array($endpoint_name, $possible_endpoints); | |
| $is_post_endpoint = in_array($endpoint_name, $post_endpoints); | |
| if($is_post_endpoint && !$is_limited_endpoint) | |
| { | |
| // Connect to db | |
| $conn = connect_db(); | |
| if($conn!=false) | |
| { | |
| // Then store data in db | |
| $q_irlt = " INSERT INTO twitter_rate_limits | |
| SET user = '".mysql_real_escape_string($tw_username)."', | |
| endpoint = '".mysql_real_escape_string($endpoint_name)."', | |
| date = '".date("Y-m-d H:i:s")."', | |
| result = '".mysql_real_escape_string($result)."', | |
| remaining = 0"; | |
| $r_irlt = mysql_query($q_irlt); | |
| if($r_irlt) | |
| { | |
| // Insert OK | |
| print_str("Rate limits checked for endpoint '".$endpoint_name."' - OK"); | |
| } | |
| else// Error when saving to db | |
| { | |
| } | |
| } | |
| // Destroy db connection | |
| disconnect_db($conn); | |
| } | |
| }// END METHOD | |
| public function getCallsCounter() | |
| { | |
| return $this->calls_counter; | |
| } | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment