Advertisment

Add Tweets to Your PhpBB-based Website

author-image
PCQ Bureau
New Update

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.

Advertisment

Direct Hit!

Applies To: PhpBB developers



Price: Free


USP: Using Twitter on a PhpBB-based forum


Primary Link: twitter.com


Keywords:
twitter api, phpbb

Twitter provides you APIs to display your status on the forum. You may update

it, retrieve user information, send direct messages and do other similar stuff.

So, first you need to understand how the Twitter API works. Here, as we shall be

posting a tweet (a short message on Twitter) for the first time, we need to

fully understand the user timeline API. This API lets you retrieve 20 latest

posts from a single user. However, it does not let you retrieve other user's

status or timeline. As Twitter is HTTP-based, you can pull or push your status

through simple HTTP GET and POST methods, which makes it easier to use. Also the

application uses REST (Representational State Transfer) style of architecture,

which is a software architecture for hypermedia systems. It is an extension to

HTTP and lets you use plain text, audio/video, graphics and hyperlinks together

for creating content. So it simplifies the process of text-based communication.

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.

Advertisment

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. In case you need more than

100 tweets per hour, then you need to authenticate yourself before retrieving

the status messages, or you can request the IP address to the 'Twitter

whitelisting account.' For adding IP address to the white listing account, copy

this URL http://twitter. com/help/request_whitelisting to the web browser and

then login using your Twitter account. 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.'

This ID can be used to retrieve tweets from Twitter.com.

Listed are tweets from people following your site.

Before you start, you require the Twitter ID for your account. The easiest

way to get it is by clicking on the RSS feed link provided on the home page. It

will then ask for your user name and password. Provide the necessary credentials

and hit Ok. 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.

Advertisment

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/.xml?count=5");


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.
Advertisment

$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.

Advertisment

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.

Advertisment