Twitter API testing"; require_once('TwitterAPIExchange.php'); require_once('upgrade.php'); //allows for compsci02.snc.edu to handle json_decode //provided by Jordan Henkel /** Set access tokens here - see: https://dev.twitter.com/apps/ **/ $settings = array( 'oauth_access_token' => /*"ENTER YOURS IN QUOTES"*/, 'oauth_access_token_secret' => /*"ENTER YOURS IN QUOTES"*/, 'consumer_key' => /*"ENTER YOURS IN QUOTES"*/, 'consumer_secret' => /*"ENTER YOURS IN QUOTES"*/ ); $url = "https://api.twitter.com/1.1/trends/place.json"; //trend by location $requestMethod = "GET"; $loc = '23424977'; $getfield = '?id='.$loc; //right now, getting trends for United States - narrow to WI //1 is world-wide, 23424977 is United States $twitter = new TwitterAPIExchange($settings); $string = json_decode($twitter->setGetfield($getfield) ->buildOauth($url, $requestMethod) ->performRequest(), true); if($string["errors"][0]["message"] != "") //make sure we received valid data from API {echo "

Sorry, there was a problem.

Twitter returned the following error message:

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

";exit();} $file = "/usr/local/data/html/cs460/2016/joskch/twitter_api/tweet_data.txt"; //this is where I will be storing the data //change this location when moving to production @$fp = fopen($file, 'ab'); if (!$fp) //make sure the data file is opened { echo "

Unable to open file. Please try again later.

"; exit; } foreach($string as $el) foreach($el['trends'] as $items) { { $nm = $items['name']; $vol = $items['tweet_volume']; echo "Name: ".$nm."
"; echo "Volume: ".$vol."
"; if ($vol != "") { $wstring = $nm."~".$vol; //'~' is a unique character set to separate name & volume fwrite($fp, $wstring, strlen($wstring)); fwrite($fp, "\n", strlen("\n")); } } } ?>