BANGALORE, INDIA: Twitter has become one of the easiest ways to communicate and provide updates to acquaintances, which is why it is so popular these days. Most organizations are using Twitter as a news broadcasting channel.
A nice way to utilize the functionality provided by the application is to integrate it with your website. In this article, we show how you can do it. But before we start, please note the code discussed here is for integrating Twitter on a PHPBB-based forum. However, the Twitter code can also be used on other forums or websites as well.
Hence for retrieving tweets one can use simple URLs, for example http://twitter.com/statuses/ user_timeline/12321.format. One can also specify the format in which tweets are required. For example, through this API you can get tweets in XML, JSON, RSS and ATOM format.
Configuring Twitter for a PhpBB-based forum
For retrieving status from the Twitter application, there are certain conditions. For example, if your status is 'protected' then you need authentication details else you will be denied access. But if the status is 'public' then you can easily get tweets. However, if you are not authenticating yourself then you can only get 100 requests per hour.
This ID can be used to retrieve tweets from Twitter.com. Listed are tweets from people following your site. |
On the home page, provide a single or multiple IPs, depending on the number of websites you want to display your Twitter account on. Next, provide the reason for whitelisting these IPs and click on 'Submit Request.'
When the RSS page opens up, check the URL. At the end of the URL you will find a number---that's your unique Twitter ID to access your tweets. Now that you know your ID, let's proceed further.
Given below is the code that will help you retrieve tweets from your Twitter account and display them on the PHPBB page.
$c = curl_init();
$auth_myself = 'pcquest';
$auth_pass='password';
curl_setopt($c, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/
curl_setopt($c,CURLOPT_USERPWD,'$auth_myself:$auth_pass');
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$src = curl_exec($c);
curl_close($c);
This is how your tweet looks on the website. You can even change the position where tweets are displayed. |
You need to whitelist the IP addresses of all websites where you want to insert your tweets. |
$tweets = new SimpleXMLElement($src);
echo $tweets->status<0>->text;
?>
We have used cURL for getting tweets. It allows you to communicate with the server over HTTP or HTTPS. cURL also supports FTP, Gopher, Telnet and LDAP protocols. In the above code, we have initialized cURL and provided authentication details. Next, we specify the URL from where tweets can be retrieved along with the Twitter ID. As we want to retrieve tweets in XML format, using simpleXML we will convert XML into an object that can be processed easily.
Now for integrating it with a PHPBB-based forum, open up the template folder > sub_silver and open the overall_header.tpl file in edit mode. In this, find the following line of code and paste it above the code given in the previous para.
TD background=homepage_files/redtile.gif>
src="homepage_files/redcorner6.gif" width=15
border=0>
Note that this code might change depending upon which version of PHPBB you are using, and modifications that you have done. Now, open your PHPBB-based forum and you should be able to see all the tweets.