All pastes #1357758 Raw Edit

twitterSearch.func.php

public php v1 · immutable
#1357758 ·published 2009-03-10 23:01 UTC
rendered paste body
<?php	/*		@func 	twitterSearch		@var 	string 	$query 		Search Query		@var 	int 	$amt 		Amount of Tweets.  Default 100, Max 100		@var 	string 	$langCode	ISO Two-letter Language Code.  Default en		$tArray(array (			(int)0 => array			(				(string)"text" => (string)"tweet text here",				(string)"to_user_id" => (int)INTEGER_OF_USER_ID_TWEET_WAS_TO,				(string)"from_user" => (string)"screenNameOfUserThatSentTheTweet",				(string)"id" => (int)INTEGER_OF_TWEETS_ID,				(string)"from_user_id" => (int)INTEGER_OF_USER_ID_TWEET_WAS_SENT_FROM,				(string)"iso_language_code" => (string)"en",				(string)"source" => (string)"the HTML referencing where the tweet was from",				(string)"profile_image_url" => (string)"http://url/to/profile/image",				(string)"created_at" => (string)"Tue, 10 Mar 2009 22:10:57 +0000" [felt it was better to use an example]			)		));	*/	function twitterSearch($query = "design OR typography OR photoshop OR tutorial",$amt = 100,$langCode = "en")	{		if (defined("DEBUG_MODE") && DEBUG) { print("Debug Mode Set\r\n"); }		// Transform the Query into a URL String			$query = urlencode($query);			$query = str_replace("%20","+",$query);		// Create the URL			$url = "http://search.twitter.com/search.json";			$url.= "?q=".$query;			if ($amt)				{ $url .= "&rpp=".$amt; }			if ($langCode)				{ $url .= "&lang=".$langCode; }		// Set the Directory and Filename			$dirname = dirname(__FILE__);			$file = $dirname."/tweets.json";		// Create the File if it doesn't exist			if (!file_exists($file))				{ if(defined("DEBUG_MODE") && DEBUG_MODE) { print("Creating File\r\n"); }$f = fopen($file,"w");fwrite($f,'{"time":0}');fclose($f); }				// Store the contents of the file			$json = file_get_contents($file);			$jArray = (array)json_decode($json);		// See if the time is too old			if (($jArray["time"]+(60*5)) < time()) /* It is */			{				if(defined("DEBUG_MODE") && DEBUG_MODE) { print("File was too Old.  Refetching\r\n"); }				$json = file_get_contents("http://search.twitter.com/search.json?q=$query"); /* Fetch Tweets */				$jArray = (array)json_decode($json); /* Decode the JSON into an Array */				$jArray["time"] = time(); /* Add the Time to the Array */				$json = json_encode($jArray); /* Recode the Array into JSON */				$f = fopen($file,"w"); /* Open the File */				fwrite($f,$json); /* Store the new JSON into the file for later use */				fclose($f); /* Close the File */			}		// Return the Array			return $jArray["results"];	}?>