Return to Snippet

Revision: 34430
at November 10, 2010 08:26 by mloberg


Updated Code
<?php
// the number of tweets we want to display
$tweetsToDisplay = 10;
$username = "username"; // make sure you change this to your username

$twitterrequest = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=' . $username . '&count=' . $tweetsToDisplay;
$twitterci = curl_init($twitterrequest);
curl_setopt($twitterci,CURLOPT_RETURNTRANSFER, TRUE);
$twitterinput = curl_exec($twitterci);
curl_close($twitterci);

// parameter 'true' is necessary for output as PHP array
$tweets = json_decode($twitterinput,true);

echo "<h3><a href=\"http://twitter.com/$username\">Twitter Updates</a></h3>";

if(empty($tweets)){
	echo "Twitter doesn't seem to be working at the moment.";
}else if(!empty($tweets['error'])){
	echo "There was an issue with Twitter.";
}else{
	// Twitter uses the GMT timezone
	date_default_timezone_set('GMT');
	// This is Twitter's response date format
	$curDate = date('D M d H:i:s O Y');
	// then we explode it to use it later
	list($curDay, $curMonth, $curDayNum, $curTime, $curTimezone, $curYear) = explode(" ",$curDate);
	$curTimeArray = explode(":", $curTime);
	// turn the month name into a number
	$curMonthNum = array("Filler","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
		for($cm=1;$cm<=12;$cm++){
			if($curMonthNum[$cm] == $curMonth){
				$curMonth = $cm;
			}
		}
	echo "<ul>";
	// a quick bug fix, without this extra bullets may be in the list
	if($tweets[0]['user']['statuses_count'] < $tweetsToDisplay){
		$display = $tweets[0]['user']['statuses_count'] - 1;
	}else{
		$display = $tweetsToDisplay - 1;
	}

	for($i=0;$i<=$display;$i++){
		echo "<li>";
		// we are going to check for any links, @users, or #hashtags
		$tweetText = $tweets[$i]['text'] . "<br />";
		if(preg_match('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@',$tweetText,$urlTweet)){
			// if there is, replace that match with an actual link
			$tweetText = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@','<a href="$1">$1</a>',$tweetText);
		}
		// check to see if it's a reply
		if(preg_match('/@([\w]+)/', $tweetText)){
			$tweetText = preg_replace('/@([\w]+)/','<a href="http://twitter.com/$1">@$1</a>',$tweetText);
		}
		// then check for a hash tag
		if(preg_match('/\B#(\w*[a-zA-Z]+\w*)/', $tweetText)){
			$tweetText = preg_replace('/\B#(\w*[a-zA-Z]+\w*)/', '<a href="http://twitter.com/search?q=%23$1">#$1</a>',$tweetText);
		}
		// Finally, we can echo the Tweet
		echo $tweetText;
		// then we figure out the time ago it was sent
		$tweetTime = $tweets[$i]['created_at'];
		list($day, $month, $dayNum, $time, $timezone, $year) = explode(" ", $tweetTime);
		$timeArray = explode(":",$time);
		// Because the month comes in as a string, we need to covert it to a integer
		$monthNum = array("Filler","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
		for($m=1;$m<=12;$m++){
			if($monthNum[$m] == $month){
				$month = $m;
			}
		}
		// and then echo it
		// if the month, day, and hour are the same, echo the minutes ago
		if($year == $curYear && $month == $curMonth && $dayNum == $curDayNum && $timeArray[0] == $curTimeArray[0]){
			$timeAgo = $curTimeArray[1] - $timeArray[1];
			if($timeAgo == 0){
				echo "Sent less then a minute ago";
			}else if($timeAgo == 1){
				echo "Sent one minute ago";
			}else if($timeAgo > 50){
				echo "Sent about an hour ago";
			}else{
				echo "Sent " . $timeAgo . " minutes ago";
			}
		// if it was less then a day ago, echo the hours ago
		}else if($year == $curYear && $month == $curMonth && $dayNum == $curDayNum){
			$timeAgo = $curTimeArray[0] - $timeArray[0];
			if($timeAgo > 22){
				echo "Sent about a day ago";
			}else if($timeAgo ==1){
				echo "Sent an hour ago";
			}else{
				echo "Sent " . $timeAgo . " hours ago";
			}
		}else if($year == $curYear && $month == $curMonth){
			$timeAgo = $curDayNum - $dayNum;
			if($timeAgo > 29){
				echo "Sent about a month ago";
			}else if($timeAgo == 1){
				echo "Sent a day ago";
			}else{
				echo "Sent " . $timeAgo . " days ago";
			}
		}else if($year == $curYear){
			$timeAgo = $curMonth - $month;
			if($timeAgo == 1){
				echo "Sent one month ago";
			}else{
				echo "Sent " . $timeAgo . " months ago";
			}
		}else{
			$timeAgo = $curYear - $year;
			if($timeAgo == 1){
				echo "Sent " . $timeAgo . " year ago";
			}else{
				echo "Sent " . $timeAgo . " years ago";
			}
		}
		// if it's a reply, append a "In reply to at the end"
		if($tweets[$i]['in_reply_to_screen_name'] !== NULL){
			echo " in reply to <a href=\"http://twitter.com/" . $tweets[$i]['in_reply_to_screen_name'] . "\">" . $tweets[$i]['in_reply_to_screen_name'] . "</a>.";
		}else{
			echo ".";
		}
		echo "</li>";
	}
	echo "</ul>";
	echo "<p><a href=\"http://twitter.com/$username\">Follow Me</a></p>";
}
?>

Revision: 34429
at November 10, 2010 08:08 by mloberg


Updated Code
<?php
// the number of tweets we want to display
$tweetsToDisplay = 10;
$username = "username"; // make sure you change this to your username

$twitterrequest = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=' . $username . '&count=' . $tweetsToDisplay;
$twitterci = curl_init($twitterrequest);
curl_setopt($twitterci,CURLOPT_RETURNTRANSFER, TRUE);
$twitterinput = curl_exec($twitterci);
curl_close($twitterci);

// parameter 'true' is necessary for output as PHP array
$tweets = json_decode($twitterinput,true);

echo "<h3><a href=\"http://twitter.com/$username\">Twitter Updates</a></h3>";

if(empty($tweets)){
	echo "Twitter doesn't seem to be working at the moment.";
}else if(!empty($tweets['error'])){
	echo "There was an issue with Twitter.";
}else{
	// Twitter uses the GMT timezone
	date_default_timezone_set('GMT');
	// This is Twitter's response date format
	$curDate = date('D M d H:i:s O Y');
	// then we explode it to use it later
	list($curDay, $curMonth, $curDayNum, $curTime, $curTimezone, $curYear) = explode(" ",$curDate);
	$curTimeArray = explode(":", $curTime);
	// turn the month name into a number
	$curMonthNum = array("Filler","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
		for($cm=1;$cm<=12;$cm++){
			if($curMonthNum[$cm] == $curMonth){
				$curMonth = $cm;
			}
		}
	echo "<ul>";
	// a quick bug fix, without this extra bullets may be in the list
	if($tweets[0]['user']['statuses_count'] < $tweetsToDisplay){
		$display = $tweets[0]['user']['statuses_count'] - 1;
	}else{
		$display = $tweetsToDisplay - 1;
	}

	for($i=0;$i<=$display;$i++){
		echo "<li>";
		// we are going to check for any links, @users, or #hashtags
		$tweetText = $tweets[$i]['text'] . "<br />";
		if(preg_match('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@',$tweetText,$urlTweet)){
			// if there is, replace that match with an actual link
			$tweetText = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@','<a href="$1">$1</a>',$tweetText);
		}
		// check to see if it's a reply
		if(preg_match('/@([\w]+)/', $tweetText)){
			$tweetText = preg_replace('/@([\w]+)/','<a href="http://twitter.com/$1">@$1</a>',$tweetText);
		}
		// then check for a hash tag
		if(preg_match('/\B#(\w*[a-zA-Z]+\w*)/', $tweetText)){
			$tweetText = preg_replace('/\B#(\w*[a-zA-Z]+\w*)/', '<a href="http://twitter.com/search?q=%23$1">#$1</a>',$tweetText);
		}
		// Finally, we can echo the Tweet
		echo $tweetText;
		// then we figure out the time ago it was sent
		$tweetTime = $tweets[$i]['created_at'];
		list($day, $month, $dayNum, $time, $timezone, $year) = explode(" ", $tweetTime);
		$timeArray = explode(":",$time);
		// Because the month comes in as a string, we need to covert it to a integer
		$monthNum = array("Filler","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
		for($m=1;$m<=12;$m++){
			if($monthNum[$m] == $month){
				$month = $m;
			}
		}
		// and then echo it
		// if the month, day, and hour are the same, echo the minutes ago
		if($year == $curYear && $month == $curMonth && $dayNum == $curDayNum && $timeArray[0] == $curTimeArray[0]){
			$timeAgo = $curTimeArray[1] - $timeArray[1];
			if($timeAgo == 0){
				echo "Sent less then a minute ago";
			}else if($timeAgo == 1){
				echo "Sent one minute ago";
			}else if($timeAgo > 50){
				echo "Sent about an hour ago";
			}else{
				echo "Sent " . $timeAgo . " minutes ago";
			}
		// if it was less then a day ago, echo the hours ago
		}else if($year == $curYear && $month == $curMonth && $dayNum == $curDayNum){
			$timeAgo = $curTimeArray[0] - $timeArray[0];
			if($timeAgo > 22){
				echo "Sent about a day ago";
			}else if($timeAgo ==1){
				echo "Sent an hour ago";
			}else{
				echo "Sent " . $timeAgo . " hours ago";
			}
		}else if($year == $curYear && $month == $curMonth){
			$timeAgo = $curDayNum - $dayNum;
			if($timeAgo > 29){
				echo "Sent about a month ago";
			}else if($timeAgo == 1){
				echo "Sent a day ago";
			}else{
				echo "Sent " . $timeAgo . " days ago";
			}
		}else if($year == $curYear){
			$timeAgo = $curMonth - $month;
			if($timeAgo == 1){
				echo "Sent one month ago";
			}else{
				echo "Sent " . $timeAgo . " months ago";
			}
		}else{
			$timeAgo = $curYear - $year;
			if($timeAgo == 1){
				echo "Sent " . $timeAgo . " year ago";
			}else{
				echo "Sent " . $timeAgo . " years ago";
			}
		}
		// if it's a reply, append a "In reply to at the end"
		if($tweets[$i]['in_reply_to_screen_name'] !== NULL){
			echo " in reply to <a href=\"http://twitter.com/$tweets[$i]['in_reply_to_screen_name']\">" . $tweets[$i]['in_reply_to_screen_name'] . "</a>.";
		}else{
			echo ".";
		}
		echo "</li>";
	}
	echo "</ul>";
	echo "<p><a href=\"http://twitter.com/$username\">Follow Me</a></p>";
}
?>

Revision: 34428
at October 23, 2010 06:50 by mloberg


Updated Code
<?php
// the number of videos we want to display
$tweetsToDisplay = 10;
$username = "username"; // make sure you change this to your username

$twitterrequest = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=' . $username . '&count=' . $tweetsToDisplay;
$twitterci = curl_init($twitterrequest);
curl_setopt($twitterci,CURLOPT_RETURNTRANSFER, TRUE);
$twitterinput = curl_exec($twitterci);
curl_close($twitterci);

// parameter 'true' is necessary for output as PHP array
$tweets = json_decode($twitterinput,true);

echo "<h3><a href=\"http://twitter.com/$username\">Twitter Updates</a></h3>";

if(empty($tweets)){
	echo "Twitter doesn't seem to be working at the moment.";
}else if(!empty($tweets['error'])){
	echo "There was an issue with Twitter.";
}else{
	// Twitter uses the GMT timezone
	date_default_timezone_set('GMT');
	// This is Twitter's response date format
	$curDate = date('D M d H:i:s O Y');
	// then we explode it to use it later
	list($curDay, $curMonth, $curDayNum, $curTime, $curTimezone, $curYear) = explode(" ",$curDate);
	$curTimeArray = explode(":", $curTime);
	// turn the month name into a number
	$curMonthNum = array("Filler","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
		for($cm=1;$cm<=12;$cm++){
			if($curMonthNum[$cm] == $curMonth){
				$curMonth = $cm;
			}
		}
	echo "<ul>";
	// a quick bug fix, without this extra bullets may be in the list
	if($tweets[0]['user']['statuses_count'] < $tweetsToDisplay){
		$display = $tweets[0]['user']['statuses_count'] - 1;
	}else{
		$display = $tweetsToDisplay - 1;
	}

	for($i=0;$i<=$display;$i++){
		echo "<li>";
		// we are going to check for any links, @users, or #hashtags
		$tweetText = $tweets[$i]['text'] . "<br />";
		if(preg_match('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@',$tweetText,$urlTweet)){
			// if there is, replace that match with an actual link
			$tweetText = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@','<a href="$1">$1</a>',$tweetText);
		}
		// check to see if it's a reply
		if(preg_match('/@([\w]+)/', $tweetText)){
			$tweetText = preg_replace('/@([\w]+)/','<a href="http://twitter.com/$1">@$1</a>',$tweetText);
		}
		// then check for a hash tag
		if(preg_match('/\B#(\w*[a-zA-Z]+\w*)/', $tweetText)){
			$tweetText = preg_replace('/\B#(\w*[a-zA-Z]+\w*)/', '<a href="http://twitter.com/search?q=%23$1">#$1</a>',$tweetText);
		}
		// Finally, we can echo the Tweet
		echo $tweetText;
		// then we figure out the time ago it was sent
		$tweetTime = $tweets[$i]['created_at'];
		list($day, $month, $dayNum, $time, $timezone, $year) = explode(" ", $tweetTime);
		$timeArray = explode(":",$time);
		// Because the month comes in as a string, we need to covert it to a integer
		$monthNum = array("Filler","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
		for($m=1;$m<=12;$m++){
			if($monthNum[$m] == $month){
				$month = $m;
			}
		}
		// and then echo it
		// if the month, day, and hour are the same, echo the minutes ago
		if($year == $curYear && $month == $curMonth && $dayNum == $curDayNum && $timeArray[0] == $curTimeArray[0]){
			$timeAgo = $curTimeArray[1] - $timeArray[1];
			if($timeAgo == 0){
				echo "Sent less then a minute ago";
			}else if($timeAgo == 1){
				echo "Sent one minute ago";
			}else if($timeAgo > 50){
				echo "Sent about an hour ago";
			}else{
				echo "Sent " . $timeAgo . " minutes ago";
			}
		// if it was less then a day ago, echo the hours ago
		}else if($year == $curYear && $month == $curMonth && $dayNum == $curDayNum){
			$timeAgo = $curTimeArray[0] - $timeArray[0];
			if($timeAgo > 22){
				echo "Sent about a day ago";
			}else if($timeAgo ==1){
				echo "Sent an hour ago";
			}else{
				echo "Sent " . $timeAgo . " hours ago";
			}
		}else if($year == $curYear && $month == $curMonth){
			$timeAgo = $curDayNum - $dayNum;
			if($timeAgo > 29){
				echo "Sent about a month ago";
			}else if($timeAgo == 1){
				echo "Sent a day ago";
			}else{
				echo "Sent " . $timeAgo . " days ago";
			}
		}else if($year == $curYear){
			$timeAgo = $curMonth - $month;
			if($timeAgo == 1){
				echo "Sent one month ago";
			}else{
				echo "Sent " . $timeAgo . " months ago";
			}
		}else{
			$timeAgo = $curYear - $year;
			if($timeAgo == 1){
				echo "Sent " . $timeAgo . " year ago";
			}else{
				echo "Sent " . $timeAgo . " years ago";
			}
		}
		// if it's a reply, append a "In reply to at the end"
		if($tweets[$i]['in_reply_to_screen_name'] !== NULL){
			echo " in reply to <a href=\"http://twitter.com/$tweets[$i]['in_reply_to_screen_name']\">" . $tweets[$i]['in_reply_to_screen_name'] . "</a>.";
		}else{
			echo ".";
		}
		echo "</li>";
	}
	echo "</ul>";
	echo "<p><a href=\"http://twitter.com/$username\">Follow Me</a></p>";
}
?>

Revision: 34427
at October 22, 2010 13:40 by mloberg


Updated Code
<?php
// the number of videos we want to display
$tweetsToDisplay = 10;
$username = "username"; // make sure you change this to your username

$twitterrequest = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=' . $username . '&count=' . $tweetsToDisplay;
$twitterci = curl_init($twitterrequest);
curl_setopt($twitterci,CURLOPT_RETURNTRANSFER, TRUE);
$twitterinput = curl_exec($twitterci);

// parameter 'true' is necessary for output as PHP array
$tweets = json_decode($twitterinput,true);

echo "<h3><a href=\"http://twitter.com/$username\">Twitter Updates</a></h3>";

if(empty($tweets)){
	echo "Twitter doesn't seem to be working at the moment.";
}else if(!empty($tweets['error'])){
	echo "There was an issue with Twitter.";
}else{
	// Twitter uses the GMT timezone
	date_default_timezone_set('GMT');
	// This is Twitter's response date format
	$curDate = date('D M d H:i:s O Y');
	// then we explode it to use it later
	list($curDay, $curMonth, $curDayNum, $curTime, $curTimezone, $curYear) = explode(" ",$curDate);
	$curTimeArray = explode(":", $curTime);
	// turn the month name into a number
	$curMonthNum = array("Filler","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
		for($cm=1;$cm<=12;$cm++){
			if($curMonthNum[$cm] == $curMonth){
				$curMonth = $cm;
			}
		}
	echo "<ul>";
	// a quick bug fix, without this extra bullets may be in the list
	if($tweets[0]['user']['statuses_count'] < $tweetsToDisplay){
		$display = $tweets[0]['user']['statuses_count'] - 1;
	}else{
		$display = $tweetsToDisplay - 1;
	}

	for($i=0;$i<=$display;$i++){
		echo "<li>";
		// we are going to check for any links, @users, or #hashtags
		$tweetText = $tweets[$i]['text'] . "<br />";
		if(preg_match('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@',$tweetText,$urlTweet)){
			// if there is, replace that match with an actual link
			$tweetText = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@','<a href="$1">$1</a>',$tweetText);
		}
		// check to see if it's a reply
		if(preg_match('/@([\w]+)/', $tweetText)){
			$tweetText = preg_replace('/@([\w]+)/','<a href="http://twitter.com/$1">@$1</a>',$tweetText);
		}
		// then check for a hash tag
		if(preg_match('/\B#(\w*[a-zA-Z]+\w*)/', $tweetText)){
			$tweetText = preg_replace('/\B#(\w*[a-zA-Z]+\w*)/', '<a href="http://twitter.com/search?q=%23$1">#$1</a>',$tweetText);
		}
		// Finally, we can echo the Tweet
		echo $tweetText;
		// then we figure out the time ago it was sent
		$tweetTime = $tweets[$i]['created_at'];
		list($day, $month, $dayNum, $time, $timezone, $year) = explode(" ", $tweetTime);
		$timeArray = explode(":",$time);
		// Because the month comes in as a string, we need to covert it to a integer
		$monthNum = array("Filler","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
		for($m=1;$m<=12;$m++){
			if($monthNum[$m] == $month){
				$month = $m;
			}
		}
		// and then echo it
		// if the month, day, and hour are the same, echo the minutes ago
		if($year == $curYear && $month == $curMonth && $dayNum == $curDayNum && $timeArray[0] == $curTimeArray[0]){
			$timeAgo = $curTimeArray[1] - $timeArray[1];
			if($timeAgo == 0){
				echo "Sent less then a minute ago";
			}else if($timeAgo == 1){
				echo "Sent one minute ago";
			}else if($timeAgo > 50){
				echo "Sent about an hour ago";
			}else{
				echo "Sent " . $timeAgo . " minutes ago";
			}
		// if it was less then a day ago, echo the hours ago
		}else if($year == $curYear && $month == $curMonth && $dayNum == $curDayNum){
			$timeAgo = $curTimeArray[0] - $timeArray[0];
			if($timeAgo > 22){
				echo "Sent about a day ago";
			}else if($timeAgo ==1){
				echo "Sent an hour ago";
			}else{
				echo "Sent " . $timeAgo . " hours ago";
			}
		}else if($year == $curYear && $month == $curMonth){
			$timeAgo = $curDayNum - $dayNum;
			if($timeAgo > 29){
				echo "Sent about a month ago";
			}else if($timeAgo == 1){
				echo "Sent a day ago";
			}else{
				echo "Sent " . $timeAgo . " days ago";
			}
		}else if($year == $curYear){
			$timeAgo = $curMonth - $month;
			if($timeAgo == 1){
				echo "Sent one month ago";
			}else{
				echo "Sent " . $timeAgo . " months ago";
			}
		}else{
			$timeAgo = $curYear - $year;
			if($timeAgo == 1){
				echo "Sent " . $timeAgo . " year ago";
			}else{
				echo "Sent " . $timeAgo . " years ago";
			}
		}
		// if it's a reply, append a "In reply to at the end"
		if($tweets[$i]['in_reply_to_screen_name'] !== NULL){
			echo " in reply to <a href=\"http://twitter.com/$tweets[$i]['in_reply_to_screen_name']\">" . $tweets[$i]['in_reply_to_screen_name'] . "</a>.";
		}else{
			echo ".";
		}
		echo "</li>";
	}
	echo "</ul>";
	echo "<p><a href=\"http://twitter.com/$username\">Follow Me</a></p>";
}
?>

Revision: 34426
at October 22, 2010 06:26 by mloberg


Updated Code
<?php
// the number of videos we want to display
$tweetsToDisplay = 10;
$username = "username"; // make sure you change this to your username

$twitterrequest = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=' . $username . '&count=' . $tweetsToDisplay;
$twitterci = curl_init($twitterrequest);
curl_setopt($twitterci,CURLOPT_RETURNTRANSFER, TRUE);
$twitterinput = curl_exec($twitterci);

// parameter 'true' is necessary for output as PHP array
$tweets = json_decode($twitterinput,true);

echo "<h3><a href=\"http://twitter.com/$username\">Twitter Updates</a></h3>";

if(empty($tweets)){
	echo "Twitter doesn't seem to be working at the moment.";
}else if(!empty($tweets['error'])){
	echo "There was an issue with Twitter.";
}else{
	// Twitter uses the GMT timezone
	date_default_timezone_set('GMT');
	// This is Twitter's response date format
	$curDate = date('D M d H:i:s O Y');
	// then we explode it to use it later
	list($curDay, $curMonth, $curDayNum, $curTime, $curTimezone, $curYear) = explode(" ",$curDate);
	$curTimeArray = explode(":", $curTime);
	// turn the month name into a number
	$curMonthNum = array("Filler","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
		for($cm=1;$cm<=12;$cm++){
			if($curMonthNum[$cm] == $curMonth){
				$curMonth = $cm;
			}
		}
	echo "<ul>";
	// a quick bug fix, without this extra bullets may be in the list
	if($tweets[0]['user']['statuses_count'] < $tweetsToDisplay){
		$display = $tweets[0]['user']['statuses_count'] - 1;
	}else{
		$display = $tweetsToDisplay - 1;
	}

	for($i=0;$i<=$display;$i++){
		echo "<li>";
		// we are going to check for any links, @users, or #hashtags
		$tweetText = $tweets[$i]['text'] . "<br />";
		if(preg_match('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@',$tweetText,$urlTweet)){
			// if there is, replace that match with an actual link
			$tweetText = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@','<a href="$1">$1</a>',$tweetText);
		}
		// check to see if it's a reply
		if(preg_match('/@([\w]+)/', $tweetText)){
			$tweetText = preg_replace('/@([\w]+)/','<a href="http://twitter.com/$1">@$1</a>',$tweetText);
		}
		// then check for a hash tag
		// coming soon
		// Finally, we can echo the Tweet
		echo $tweetText;
		// then we figure out the time ago it was sent
		$tweetTime = $tweets[$i]['created_at'];
		list($day, $month, $dayNum, $time, $timezone, $year) = explode(" ", $tweetTime);
		$timeArray = explode(":",$time);
		// Because the month comes in as a string, we need to covert it to a integer
		$monthNum = array("Filler","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
		for($m=1;$m<=12;$m++){
			if($monthNum[$m] == $month){
				$month = $m;
			}
		}
		// and then echo it
		// if the month, day, and hour are the same, echo the minutes ago
		if($year == $curYear && $month == $curMonth && $dayNum == $curDayNum && $timeArray[0] == $curTimeArray[0]){
			$timeAgo = $curTimeArray[1] - $timeArray[1];
			if($timeAgo == 0){
				echo "Sent less then a minute ago";
			}else if($timeAgo == 1){
				echo "Sent one minute ago";
			}else if($timeAgo > 50){
				echo "Sent about an hour ago";
			}else{
				echo "Sent " . $timeAgo . " minutes ago";
			}
		// if it was less then a day ago, echo the hours ago
		}else if($year == $curYear && $month == $curMonth && $dayNum == $curDayNum){
			$timeAgo = $curTimeArray[0] - $timeArray[0];
			if($timeAgo > 22){
				echo "Sent about a day ago";
			}else if($timeAgo ==1){
				echo "Sent an hour ago";
			}else{
				echo "Sent " . $timeAgo . " hours ago";
			}
		}else if($year == $curYear && $month == $curMonth){
			$timeAgo = $curDayNum - $dayNum;
			if($timeAgo > 29){
				echo "Sent about a month ago";
			}else if($timeAgo == 1){
				echo "Sent a day ago";
			}else{
				echo "Sent " . $timeAgo . " days ago";
			}
		}else if($year == $curYear){
			$timeAgo = $curMonth - $month;
			if($timeAgo == 1){
				echo "Sent one month ago";
			}else{
				echo "Sent " . $timeAgo . " months ago";
			}
		}else{
			$timeAgo = $curYear - $year;
			if($timeAgo == 1){
				echo "Sent " . $timeAgo . " year ago";
			}else{
				echo "Sent " . $timeAgo . " years ago";
			}
		}
		// if it's a reply, append a "In reply to at the end"
		if($tweets[$i]['in_reply_to_screen_name'] !== NULL){
			echo " in reply to <a href=\"http://twitter.com/$tweets[$i]['in_reply_to_screen_name']\">" . $tweets[$i]['in_reply_to_screen_name'] . "</a>.";
		}else{
			echo ".";
		}
		echo "</li>";
	}
	echo "</ul>";
	echo "<p><a href=\"http://twitter.com/$username\">Follow Me</a></p>";
}
?>

Revision: 34425
at October 22, 2010 06:22 by mloberg


Updated Code
<?php
// the number of videos we want to display
$tweetsToDisplay = 10;
$username = $text;

$twitterrequest = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=' . $username . '&count=' . $tweetsToDisplay;
$twitterci = curl_init($twitterrequest);
curl_setopt($twitterci,CURLOPT_RETURNTRANSFER, TRUE);
$twitterinput = curl_exec($twitterci);

// parameter 'true' is necessary for output as PHP array
$tweets = json_decode($twitterinput,true);

echo "<h3><a href=\"http://twitter.com/$username\">Twitter Updates</a></h3>";

if(empty($tweets)){
	echo "Twitter doesn't seem to be working at the moment.";
}else if(!empty($tweets['error'])){
	echo "There was an issue with Twitter.";
}else{
	// Twitter uses the GMT timezone
	date_default_timezone_set('GMT');
	// This is Twitter's response date format
	$curDate = date('D M d H:i:s O Y');
	// then we explode it to use it later
	list($curDay, $curMonth, $curDayNum, $curTime, $curTimezone, $curYear) = explode(" ",$curDate);
	$curTimeArray = explode(":", $curTime);
	// turn the month name into a number
	$curMonthNum = array("Filler","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
		for($cm=1;$cm<=12;$cm++){
			if($curMonthNum[$cm] == $curMonth){
				$curMonth = $cm;
			}
		}
	echo "<ul>";
	// a quick bug fix, without this extra bullets may be in the list
	if($tweets[0]['user']['statuses_count'] < $tweetsToDisplay){
		$display = $tweets[0]['user']['statuses_count'] - 1;
	}else{
		$display = $tweetsToDisplay - 1;
	}

	for($i=0;$i<=$display;$i++){
		echo "<li>";
		// we are going to check for any links, @users, or #hashtags
		$tweetText = $tweets[$i]['text'] . "<br />";
		if(preg_match('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@',$tweetText,$urlTweet)){
			// if there is, replace that match with an actual link
			$tweetText = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@','<a href="$1">$1</a>',$tweetText);
		}
		// check to see if it's a reply
		if(preg_match('/@([\w]+)/', $tweetText)){
			$tweetText = preg_replace('/@([\w]+)/','<a href="http://twitter.com/$1">@$1</a>',$tweetText);
		}
		// then check for a hash tag
		// coming soon
		// Finally, we can echo the Tweet
		echo $tweetText;
		// then we figure out the time ago it was sent
		$tweetTime = $tweets[$i]['created_at'];
		list($day, $month, $dayNum, $time, $timezone, $year) = explode(" ", $tweetTime);
		$timeArray = explode(":",$time);
		// Because the month comes in as a string, we need to covert it to a integer
		$monthNum = array("Filler","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
		for($m=1;$m<=12;$m++){
			if($monthNum[$m] == $month){
				$month = $m;
			}
		}
		// and then echo it
		// if the month, day, and hour are the same, echo the minutes ago
		if($year == $curYear && $month == $curMonth && $dayNum == $curDayNum && $timeArray[0] == $curTimeArray[0]){
			$timeAgo = $curTimeArray[1] - $timeArray[1];
			if($timeAgo == 0){
				echo "Sent less then a minute ago";
			}else if($timeAgo == 1){
				echo "Sent one minute ago";
			}else if($timeAgo > 50){
				echo "Sent about an hour ago";
			}else{
				echo "Sent " . $timeAgo . " minutes ago";
			}
		// if it was less then a day ago, echo the hours ago
		}else if($year == $curYear && $month == $curMonth && $dayNum == $curDayNum){
			$timeAgo = $curTimeArray[0] - $timeArray[0];
			if($timeAgo > 22){
				echo "Sent about a day ago";
			}else if($timeAgo ==1){
				echo "Sent an hour ago";
			}else{
				echo "Sent " . $timeAgo . " hours ago";
			}
		}else if($year == $curYear && $month == $curMonth){
			$timeAgo = $curDayNum - $dayNum;
			if($timeAgo > 29){
				echo "Sent about a month ago";
			}else if($timeAgo == 1){
				echo "Sent a day ago";
			}else{
				echo "Sent " . $timeAgo . " days ago";
			}
		}else if($year == $curYear){
			$timeAgo = $curMonth - $month;
			if($timeAgo == 1){
				echo "Sent one month ago";
			}else{
				echo "Sent " . $timeAgo . " months ago";
			}
		}else{
			$timeAgo = $curYear - $year;
			if($timeAgo == 1){
				echo "Sent " . $timeAgo . " year ago";
			}else{
				echo "Sent " . $timeAgo . " years ago";
			}
		}
		// if it's a reply, append a "In reply to at the end"
		if($tweets[$i]['in_reply_to_screen_name'] !== NULL){
			echo " in reply to <a href=\"http://twitter.com/$tweets[$i]['in_reply_to_screen_name']\">" . $tweets[$i]['in_reply_to_screen_name'] . "</a>.";
		}else{
			echo ".";
		}
		echo "</li>";
	}
	echo "</ul>";
	echo "<p><a href=\"http://twitter.com/$username\">Follow Me</a></p>";
}
?>

Revision: 34424
at October 22, 2010 05:48 by mloberg


Updated Code
<?php
// the number of videos we want to display
$tweetsToDisplay = 10;
$username = "username"; // make sure to change this to your username

$twitterrequest = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=' . $username . '&count=' . $tweetsToDisplay;
$twitterci = curl_init($twitterrequest);
curl_setopt($twitterci,CURLOPT_RETURNTRANSFER, TRUE);
$twitterinput = curl_exec($twitterci);

// parameter 'true' is necessary for output as PHP array
$tweets = json_decode($twitterinput,true);

// Twitter uses the GMT timezone
date_default_timezone_set('GMT');
// This is Twitter's response date format
$curDate = date('D M d H:i:s O Y');
// then we explode it to use it later
list($curDay, $curMonth, $curDayNum, $curTime, $curTimezone, $curYear) = explode(" ",$curDate);
$curTimeArray = explode(":", $curTime);
// turn the month name into a number
$curMonthNum = array("Filler","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	for($cm=1;$cm<=12;$cm++){
		if($curMonthNum[$cm] == $curMonth){
			$curMonth = $cm;
		}
	}
echo "<h3><a href=\"http://twitter.com/$username\">Twitter Updates</a></h3>";
echo "<ul>";
// a quick bug fix, without this extra bullets may be in the list
if($tweets[0]['user']['statuses_count'] < $tweetsToDisplay){
	$display = $tweets[0]['user']['statuses_count'] - 1;
}else{
	$display = $tweetsToDisplay - 1;
}

for($i=0;$i<=$display;$i++){
	echo "<li>";
	// we are going to check for any links, @users, or #hashtags
	$tweetText = $tweets[$i]['text'] . "<br />";
	if(preg_match('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@',$tweetText,$urlTweet)){
		// if there is, replace that match with an actual link
		$tweetText = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@','<a href="$1">$1</a>',$tweetText);
	}
	// check to see if it's a reply
	if(preg_match('/@([\w]+)/', $tweetText)){
		$tweetText = preg_replace('/@([\w]+)/','<a href="http://twitter.com/$1">@$1</a>',$tweetText);
	}
	// then check for a hash tag
	
	// Finally, we can echo the Tweet
	echo $tweetText;
	// then we figure out the time ago it was sent
	$tweetTime = $tweets[$i]['created_at'];
	list($day, $month, $dayNum, $time, $timezone, $year) = explode(" ", $tweetTime);
	$timeArray = explode(":",$time);
	// Because the month comes in as a string, we need to covert it to a integer
	$monthNum = array("Filler","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	for($m=1;$m<=12;$m++){
		if($monthNum[$m] == $month){
			$month = $m;
		}
	}
	// and then echo it
	// if the month, day, and hour are the same, echo the minutes ago
	if($year == $curYear && $month == $curMonth && $dayNum == $curDayNum && $timeArray[0] == $curTimeArray[0]){
		$timeAgo = $curTimeArray[1] - $timeArray[1];
		if($timeAgo == 0){
			echo "Sent less then a minute ago";
		}else if($timeAgo == 1){
			echo "Sent one minute ago";
		}else if($timeAgo > 50){
			echo "Sent about an hour ago";
		}else{
			echo "Sent " . $timeAgo . " minutes ago";
		}
	// if it was less then a day ago, echo the hours ago
	}else if($year == $curYear && $month == $curMonth && $dayNum == $curDayNum){
		$timeAgo = $curTimeArray[0] - $timeArray[0];
		if($timeAgo > 22){
			echo "Sent about a day ago";
		}else if($timeAgo ==1){
			echo "Sent an hour ago";
		}else{
			echo "Sent " . $timeAgo . " hours ago";
		}
	}else if($year == $curYear && $month == $curMonth){
		$timeAgo = $curDayNum - $dayNum;
		if($timeAgo > 29){
			echo "Sent about a month ago";
		}else if($timeAgo == 1){
			echo "Sent a day ago";
		}else{
			echo "Sent " . $timeAgo . " days ago";
		}
	}else if($year == $curYear){
		$timeAgo = $curMonth - $month;
		if($timeAgo == 1){
			echo "Sent one month ago";
		}else{
			echo "Sent " . $timeAgo . " months ago";
		}
	}else{
		$timeAgo = $curYear - $year;
		if($timeAgo == 1){
			echo "Sent " . $timeAgo . " year ago";
		}else{
			echo "Sent " . $timeAgo . " years ago";
		}
	}
	// if it's a reply, append a "In reply to at the end"
	if($tweets[$i]['in_reply_to_screen_name'] !== NULL){
		echo " in reply to <a href=\"http://twitter.com/$tweets[$i]['in_reply_to_screen_name']\">" . $tweets[$i]['in_reply_to_screen_name'] . "</a>.";
	}else{
		echo ".";
	}
	echo "</li>";
}
echo "</ul>";
echo "<p><a href=\"http://twitter.com/$username\">Follow Me</a></p>"
?>

Revision: 34423
at October 22, 2010 05:37 by mloberg


Initial Code
<?php
// the number of videos we want to display
$tweetsToDisplay = 10;
$username = "username"; // make sure to change this to your username

$twitterrequest = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=' . $username . '&count=' . $tweetsToDisplay;
$twitterci = curl_init($twitterrequest);
curl_setopt($twitterci,CURLOPT_RETURNTRANSFER, TRUE);
$twitterinput = curl_exec($twitterci);

// parameter 'true' is necessary for output as PHP array
$tweets = json_decode($twitterinput,true);

// Twitter uses the GMT timezone
date_default_timezone_set('GMT');
// This is Twitter's response date format
$curDate = date('D M d H:i:s O Y');
// then we explode it to use it later
list($curDay, $curMonth, $curDayNum, $curTime, $curTimezone, $curYear) = explode(" ",$curDate);
$curTimeArray = explode(":", $curTime);
// turn the month name into a number
$curMonthNum = array("Filler","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	for($cm=1;$cm<=12;$cm++){
		if($curMonthNum[$cm] == $curMonth){
			$curMonth = $cm;
		}
	}
echo "<h3><a href=\"http://twitter.com/$username\">Twitter Updates</a></h3>";
echo "<ul>";
// a quick bug fix, without this an extra bullet would be in the list
$display = $tweetsToDisplay - 1;

for($i=0;$i<=$display;$i++){
	echo "<li>";
	// we are going to check for any links, @users, or #hashtags
	$tweetText = $tweets[$i]['text'] . "<br />";
	if(preg_match('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@',$tweetText,$urlTweet)){
		// if there is, replace that match with an actual link
		$tweetText = preg_replace('@(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)@','<a href="$1">$1</a>',$tweetText);
	}
	// check to see if it's a reply
	if(preg_match('/@([\w]+)/', $tweetText)){
		$tweetText = preg_replace('/@([\w]+)/','<a href="http://twitter.com/$1">@$1</a>',$tweetText);
	}
	// then check for a hash tag
	
	// Finally, we can echo the Tweet
	echo $tweetText;
	// then we figure out the time ago it was sent
	$tweetTime = $tweets[$i]['created_at'];
	list($day, $month, $dayNum, $time, $timezone, $year) = explode(" ", $tweetTime);
	$timeArray = explode(":",$time);
	// Because the month comes in as a string, we need to covert it to a integer
	$monthNum = array("Filler","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	for($m=1;$m<=12;$m++){
		if($monthNum[$m] == $month){
			$month = $m;
		}
	}
	// and then echo it
	// if the month, day, and hour are the same, echo the minutes ago
	if($year == $curYear && $month == $curMonth && $dayNum == $curDayNum && $timeArray[0] == $curTimeArray[0]){
		$timeAgo = $curTimeArray[1] - $timeArray[1];
		if($timeAgo == 0){
			echo "Sent less then a minute ago";
		}else if($timeAgo == 1){
			echo "Sent one minute ago";
		}else if($timeAgo > 50){
			echo "Sent about an hour ago";
		}else{
			echo "Sent " . $timeAgo . " minutes ago";
		}
	// if it was less then a day ago, echo the hours ago
	}else if($year == $curYear && $month == $curMonth && $dayNum == $curDayNum){
		$timeAgo = $curTimeArray[0] - $timeArray[0];
		if($timeAgo > 22){
			echo "Sent about a day ago";
		}else if($timeAgo ==1){
			echo "Sent an hour ago";
		}else{
			echo "Sent " . $timeAgo . " hours ago";
		}
	}else if($year == $curYear && $month == $curMonth){
		$timeAgo = $curDayNum - $dayNum;
		if($timeAgo > 29){
			echo "Sent about a month ago";
		}else if($timeAgo == 1){
			echo "Sent a day ago";
		}else{
			echo "Sent " . $timeAgo . " days ago";
		}
	}else if($year == $curYear){
		$timeAgo = $curMonth - $month;
		if($timeAgo == 1){
			echo "Sent one month ago";
		}else{
			echo "Sent " . $timeAgo . " months ago";
		}
	}else{
		$timeAgo = $curYear - $year;
		if($timeAgo == 1){
			echo "Sent " . $timeAgo . " year ago";
		}else{
			echo "Sent " . $timeAgo . " years ago";
		}
	}
	// if it's a reply, append a "In reply to at the end"
	if($tweets[$i]['in_reply_to_screen_name'] !== NULL){
		echo " in reply to <a href=\"http://twitter.com/$tweets[$i]['in_reply_to_screen_name']\">" . $tweets[$i]['in_reply_to_screen_name'] . "</a>.";
	}else{
		echo ".";
	}
	echo "</li>";
}
echo "</ul>";
echo "<p><a href=\"http://twitter.com/$username\">Follow Me</a></p>"
?>

Initial URL


Initial Description
A Twitter pull-in that I coded using PHP. Uses the JSON user_timeline Twitter API call. It displays the tweet with links to any urls, users, or hashtags, the time ago, and if it was in reply to anyone.

Initial Title
Twitter PHP Script

Initial Tags
php, json, twitter

Initial Language
PHP