Making Your Own Twitter Page With Simple API Calls
Say your company has blocked twitter and/or you don’t like all the extra bloat on the twitter home page. I have found it very useful to create my own page. Even though facebook and other social networking sites are accessible, and even more ironically some of my companies own advertising actually promotes following them on twitter, the site is blocked on our network. But, here’s a simple work around I created.
Using some simple API calls to twitter, I’m able to recreate my twitter feed home page, and gather some basic information about myself and my followers. The page works fine behind the firewall because the twitter calls are actually being done from my host (godaddy).
Here’s what I wanted:
- Simple feed (possibly with icons) of the people I follow.
- Ability to add people I want to follow
- Some basic information about the people I follow, and about myself (Number of followers, time, etc.)
Here are the API’s and functions I used to create my own (useable behind a firewall) version of twitter.
// Adds ‘friendname’ to the list of people you follow.
// Updates your ownstatus. Add status=’foobar’ as a post field to update.
// Get list of last 50 updates from your friends.
// Get various information about yourself. (Number of followers, number of people following you, your number of tweets.
To make these API calls, I used PHP functions of curl. Most of the calls are all the same, and involve these simple steps:
$curl = curl_init();
curl_setopt( $curl, CURLOPT, ‘http://link.to.api.page’);
curl_setopt( $curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt( $curl, CURLOPT_USERPWD, username:password); //your twitter username/password
curl_setopt( $curl, CURLOPT_POSTFIELDS, “”); //empty string to just make the call. Populate if you want to send something, such as ‘status’.
$result = curl_exec( $curl );
curl_close( $curl );
When printing the list of status, I added similar functionality to the ‘@’ and ‘#’ symbols, I used these functions:
// Add links to strings that look like links.
="$str = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\" target=_blank>\\0</a>", $str );"?>
// pull out the words with # in the beginning for use with searching (not implemented yet)
$str = preg_replace( “/B#w*b/i”, “<a href='http://twitter.com/#search?q=$0' target='_blank'>$0</a>”, $str );
//get strings that start with @ to follow them.
$str = preg_replace( “/B@w*b/i”, “<a href='t.php?follow=\0' title='Click to Follow'>$0</a>”, $str );