where's the salt?

04/19
2010

Add a Twitter Feed with PHP (3,055 views)

Add a twitter feed to your website

In the last post (Add a Twitter Feed with jQuery) we talked about how to add a twitter feed with jQuery, now it’s time to do the same with PHP. The whole idea was to get the same result by using similar methods provided by the Twitter API – and again it’s easier than you might think.

All you need is:

the twitter username/userID/screenname (preferably your own) of the feed you want to embed
and a few lines of PHP code.

The PHP:

$username = "REPLACE_WITH_YOUR_USERNAME";
    $limit = 5;
    $feed = 'http://twitter.com/statuses/user_timeline.rss?screen_name='.$username.'&count='.$limit;
    $tweets = file_get_contents($feed);
    $tweet = explode("", $tweets);
    $tcount = count($tweet) - 1;

for ($i = 1; $i <= $tcount; $i++) {
    $endtweet = explode("", $tweet[$i]);
    $title = explode("", $endtweet[0]);
    $content = explode("", $title[1]);
	$content[0] = str_replace("–", "—", $content[0]);
	$content[0] = str_replace("—", "—", $content[0]);

	$content[0] = preg_replace("/(http:\/\/|(www\.))(([^\s<]{4,68})[^\s<]*)/", '$1$2$4', $content[0]);
	$content[0] = str_replace("$username: ", "", $content[0]);
	$content[0] = preg_replace("/@(\w+)/", "@\\1", $content[0]);
	$content[0] = preg_replace("/#(\w+)/", "#\\1", $content[0]);

    $mytweets[] = $content[0];
}

while (list(, $v) = each($mytweets)) {
	$tweetout .= "
$v
\n";


The HTML (body):


The div with the id phptweets is the container which will hold our tweets; plus we use some shorthand PHP to output the tweets ($tweetout).

Twitter’s API provides a multitude of access options, again I am using the user_timeline method for the user specified in screen_name but this time I chose rss output (as opposed to json for jQuery) to fetch the tweets.

Using a loop, the script iterates through the amount of tweets specified in limit($limit=5) and for every tweet performs additional tasks (yeah, regular expressions) to transform all links, hashtags and usernames into clickable links and adding them to the array $mytweets.

I separated the creation of the created links (adding new items to the $mytweets array) and the actual output ($tweetout) to make it easier to style the output.

Again – I could have used the search method which outputs clickable links, but Twitter only makes 2 weeks worth of tweets available via the search function (specifying rpp=10 won’t help if he tweets are more than 2 weeks old).

Add some stylesheets and put the div anywhere you want your tweets to get displayed on your page.

a, a:link, a:visited, a:hover {
	text-decoration:underline;
	color:#cacaca;
}
#phptweets {
	width:650px;
	height:250px;
	background:transparent url('tbird.png') top  left no-repeat;
	border:1px solid #555555;
	text-align:left;
}
h1 {
	font-size:48px;
	font-weight:normal;
	font-family: Century Gothic,Myriad Pro,Arial,Helvetica,sans-serif;
	margin: 0 0 40px 0;
}
.twitter {
	display:table-cell; vertical-align:middle;
	padding:0 0 0 250px;
}

Tags:

Date posted: Monday, April 19th, 2010 at 8:24 am (4 months, 22 days ago.)
Posted in: business mix, tech mix
Comments RSS Feed Comments RSS Feed
Reply
Ttrackback
Print This Post Print This Post

2 Responses to “Add a Twitter Feed with PHP”

  1. 1

    [...] This post was mentioned on Twitter by ninanet. ninanet said: Add a Twitter Feed with PHP http://bit.ly/bn10CK #feed #user_timeline [...]

    Tweets that mention Add a Twitter Feed with PHP -- Topsy.com on April 19th, 2010 at 8:34 am
  2. 2

    [...] Add a Twitter Feed with PHP [...]

    ยป D&G WOMAN FASHION SHOW WINTER 2011 FTKW Blog | Fashion Beauty Wisdom on April 19th, 2010 at 9:10 am

Leave a Reply


search

Categories