Posts tagged ‘twitter’

April 28th, 2010

Simple PHP Twitter script

UPDATE: The new Twitter API v1.1 no longer supports this code. You will need to authenticate before using it. Here is a little tutorial on how to do that

I broke out this useful little PHP Twitter script from the WordPress theme jessica-fletcher-redux, which happens to be the theme I used as a template for this blog. There were a few issues I needed to overcome in order to make this work. I’m hosting this WordPress blog on Windows Server running IIS7. By default many PHP functions are disabled for security so you have to implement a work around.

The get_url_contents($url) function was added to overcome a security problem on IIS where file_get_contents() is not allowed. The other issues to is to make sure you grant write permission to PHP so it can write to your twittercache.xml file. These two changes make for a very clean twitter readout that can easily be styled using CSS.


function get_url_contents($url){
        $crl = curl_init();
        $timeout = 5;
        curl_setopt ($crl, CURLOPT_URL,$url);
        curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
        $ret = curl_exec($crl);
        return $ret;

function generatetwitter($twittername){

	$username = $twittername; // Your twitter username.
	if ($username == ""){
		$username = "discorax";
	$limit = "15"; // Number of tweets to pull in.
	/* These prefixes and suffixes will display before and after the entire block of tweets. */
	$prefix = ""; // Prefix - some text you want displayed before all your tweets.
	$suffix = ""; // Suffix - some text you want displayed after all your tweets.
	$tweetprefix = "<li>"; // Tweet Prefix - some text you want displayed before each tweet.
	$tweetsuffix = "</li> \r"; // Tweet Suffix - some text you want displayed after each tweet.
	$feed_string = "" . $username . "&rpp=" . $limit;  

 function parse_feed($feed, $prefix, $tweetprefix, $tweetsuffix, $suffix, $username) {
		$feed = str_replace("<", "<", $feed);   		$feed = str_replace(">", ">", $feed);
		$clean = explode("<content type=\"html\">", $feed);
		$link = explode(",2005:", $feed);
		$amount = count($clean) - 1;
		echo $prefix;
			for ($i = 1; $i <= $amount; $i++) {   				$cleaner = explode("</content>", $clean[$i]);
				$linker = explode("</id>", $link[$i+1]);
				$this_tweets = str_replace("&apos;", "'", $cleaner[0]);
				$this_tweets = str_replace("&gt;", ">", $this_tweets);
				$this_tweets = preg_replace('!<a href.*?>!', '', $this_tweets);
				$this_tweets = preg_replace('!</a>!', '', $this_tweets);
				echo $tweetprefix.'<a href="'.$username.'/status/';
				echo $linker[0];
				echo '" target="_blank">'.$this_tweets.'</a>';
				echo $tweetsuffix;
		echo $suffix;

	$twitter_cache_file = 'twittercache.xml';

	$mtime= null;

	if($mtime== null ||(time()-$mtime)>(9*60)){// 9 minutes or older?

	if (!@file_get_contents($twitter_cache_file)){
		  echo __('Oops, something went wrong. Did you rename the cache folder? The cache file? You shouldn\'t have done that...', 'jfr_theme');
	}else {
		  $twitterFeed = file_get_contents($twitter_cache_file);

	parse_feed($twitterFeed, $prefix, $tweetprefix, $tweetsuffix, $suffix, $username);  


Setup is easy. You simply need to call the generatetwitter() function like so.

echo "<div>";
echo "<ul>";
echo "</ul></div>";

When you’re uploading the files to your server make sure to include a blank twittercache.xml file in the same directory as the script. You can change this location by changing the value of the $twitter_cache_file variable (line 48 above) to a path where you keep the twittercache.xml file (make sure you grant write permission to that folder).

That’s it. It’s a clean little code snippit for pulling a user’s twitter feed which you can easily extend using a simple form to select your twitter account. Happy coding.

October 7th, 2009

What TweetStats says about you.

Have you visited yet? Well I guess the first question would be have you visited and set up your Twitter account? If you haven’t you can stop reading now.

If you have, and are curious about your Twitter usage, take a trip to Tweetstats and enter your account name. The service will go and fetch all tweets and build some fun Flash graphs to illustrate how you use Twitter. For instance you’ll notice that March of this year was a particularly busy tweeting month of me. I was attending MIX and letting everyone know all the cool things I was learning. You’ll also notice that I pretty much only tweet during the day. Mostly in the morning as I review all the cool stuff from the previous day.

Curious, here are my results:

There is also my TweetCloud featuring the words I tweet the most.

any my HashCloud featuring the words I hash the most.

My TweetStats give a glimpse into how I use Twitter. What will TweetStats say about you? Also, if all this Twitter Speak hash tags and @mentions is confusing you, check out Twitter Speak – A Beginners Guide to learn the basics! Happy Tweeting!

March 11th, 2009

Twitter Mosaic Design…

Want to follow me on Twitter?

Be a part of something special. Like this sweet Twitter Mosaic Design!