"", 'oauth_access_token_secret' => "", 'consumer_key' => "", 'consumer_secret' => "" ); //Set initial values $count = 0; //number of tweets currently processed $max_number_of_tweets = 5000; //number of tweets desired $sinceID = 0; //used to make sure that tweets are gathered in chronological order $maxid = -1; //used to make sure that tweets are gathered in chronological order $most_recent_date = ""; //used so we can write this information to the information file $earliest_date = ""; //used so we can write this information to the information file //separating these initially makes it easier to write the query to the file later //it also makes it slightly easier to include retweets if someone wants to change it $searchon = "#Build2015"; //what hashtag or word we want to search on $filter = "-filter:retweets"; //eliminates retweets; set to "" if you want to include retweets $search = $searchon.$filter; //concatenate so we can include in our query string //set url and variables for what we want to get from twitter $url = "https://api.twitter.com/1.1/search/tweets.json"; $requestMethod = "GET"; $getfield = "?q=$search&count=100&lang=en"; while($count < $max_number_of_tweets) { //getting the data from twitter $twitter = new TwitterAPIExchange($settings); $string = json_decode($twitter->setGetfield($getfield) ->buildOauth($url, $requestMethod) ->performRequest(),$assoc = TRUE); //make sure there are no errors if($string["errors"][0]["message"] != "") { echo "

Sorry, there was a problem.

Twitter returned the following error message:

".$string[errors][0]["message"]."

"; exit(); } //Open the files for writing $file1 = fopen("Data/tweets.txt", "a"); //for c# $file2 = fopen("Data/input.txt", "a"); //for c++ //for each tweet, implode required data and write it to the file foreach ($string['statuses'] as $items) { //the first tweet will be the most recent //the last tweet will be the earliest //constantly write over the earliest date until the script ends if($count == 0) $most_recent_date = $items['created_at']; else $earliest_date = $items['created_at']; //find largest sinceid if($items['id'] > $sinceID) $sinceID = $items['id'];; //find smallest maxid if($maxid == -1) $maxid = $items['id']; else if($maxid > $items['id']) $maxid = $items['id']; //set the retweets and favorites values $retweets = $items['retweet_count']; $favorites = $items['favorite_count']; //sometimes, it fails to bring back a value for retweets and favorites //catch that error here by setting anything that is not set to 0 if($retweets == "") $retweets = 0; if($favorites == "") $favorites = 0; //alter the tweet text to make it go through the C++ application without any issues //if the tweet text has any kind of line break, we will ignore it since that will mess up the C++ application if(!strpos($items['text'],"\r\n") && !strpos($items['text'], "\n") && !strpos($items['text'],"\n\r") && !strpos($items['text'],"\r")) { $tweet = $items['text']; //&'s, <'s, >'s come back strangley. Fix that here $tweet = str_replace("&", "&", $tweet); //&'s that come up as & $tweet = str_replace("<", "<", $tweet); $tweet = str_replace(">", ">", $tweet); //write the tweet and the tweet information to an array //this array will be written to a file for the C# application $arr1 = array($items['created_at'],"@".$items['user']['screen_name'],$retweets,$favorites,$tweet."\n"); //replace any punctuation in the tweet text $tweet = str_replace(".", "", $tweet); //periods $tweet = str_replace("!", "", $tweet); //exclamation points $tweet = str_replace(",", "", $tweet); //commas $tweet = str_replace("\"", "", $tweet); //double quotes $tweet = str_replace("'", "", $tweet); //single quotes $tweet = str_replace("&", "", $tweet); //&'s $tweet = str_replace("?", "", $tweet); //question marks $tweet = str_replace("<", "", $tweet); //< $tweet = str_replace(";", "", $tweet); //semi colons $tweet = str_replace(":", "", $tweet); //colons --this also disables URLs $tweet = str_replace("-", "", $tweet); //dashes $tweet = str_replace("(", "", $tweet); //left parenthesis $tweet = str_replace(")", "", $tweet); //right parenthesis //make array of the information that has to be stored to the file //this array will be used for the C++ application $arr2 = array($items['created_at'],"@".$items['user']['screen_name'],$retweets,$favorites,$tweet."\n"); //combine the array into one string for the file $line_for_file1 = implode("\t", $arr1); $line_for_file2 = implode("\t", $arr2); //write to the file fwrite($file1, $line_for_file1); fwrite($file2, $line_for_file2); //increment the count $count++; } } //close the files fclose($file1); fclose($file2); //sleep for a minute and then get more tweets //twitter allows 180 queries every 15 minutes, which works out to be 1 query every 5 seconds sleep(5); //make another query $url = "https://api.twitter.com/1.1/search/tweets.json"; $requestMethod = "GET"; $getfield = "?q=$search&count=100&max_id=$maxid&since_ID=$sinceID&lang=en"; } //write basic information to the information file $file = fopen("Data/information.txt", "a"); fwrite($file, $searchon."\n"); //phrase searched on fwrite($file, $count."\n"); //number of tweets processed fwrite($file, $most_recent_date."\n"); //date/time of most recent tweet fwrite($file, $earliest_date); //date/time of earliest tweet fclose($file); ?>