04/19
2010
Add a Twitter Feed with PHP (72,402 views)
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):
Twitter Feed with PHP
<?=$tweetout;?>
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; }
[…] This post was mentioned on Twitter by ninanet. ninanet said: Add a Twitter Feed with PHP http://bit.ly/bn10CK #feed #user_timeline […]
[…] Add a Twitter Feed with PHP […]
A very simple solution to a genuine problem Ive been having.
Thanks for your help
Great little script, now using it on our site. Thanks a lot.
Can you update this to include the tweet date and time?
Thanks for this, saved me some time today and was easily modifiable to reformat the twitter pull to grab a date line, link back, etc etc. Thanks again!
This worked great the first time I used it. But after refreshing, it displays nothing now. Anyone else run into this?
Works great. Only one suggestion. If you modify your hashtags regexp from:
preg_replace(“/#(\w+)/”, “….
to:
preg_replace(“/#(\w+[^;]\s)/”, “….
you’ll avoid translating special characters (accents, etc) to hiperlinks.
Many thanks for the script, and special thanks to Eric, with his tip, i managed to display french charcters correctly.
if im using this with wordpress where do I put the php code?
Thanks for sharing, works a treat on the mesh band website… http://www.themeshband.co.uk . Good work!
how can I get the date. I tried many keywords but it does not work. ex: created_at, date, datetimes, created_date. Many thanks for the answer
Hi,
you can get the date by querying created_at.
Then simply apply any formatting to it to display it the way you want it
e.g.
$mydate = [insert thedateobject for the tweet];
list($wday.', '.$month.', '.$day.', '.$year) = sscanf($mydate, "%s %s %d %s %s %d");
$dateoutput = $wday.' '.$month.', '.$day.', '.$year;
My php is so so. Could you update the post with a an alternate version which has the time stamp in it?
Hi Ethan,
I don’t think a UNIX timestamp will do much good for your website visitors.
Also, I’m not sure where you would like to use/apply it.
Nina
Hi, im getting an error
Notice: Undefined variable: tweetout in C:\xampp\htdocs\twit\twitterfeed-php.php on line 27
Do you know how i can fix it?
thanks
It’s technically not an error, but a “notice”.
I was sloppy when writing the code and didn’t explicitly declare the variable before using it.
You can solve the problem by simply adding
$tweetout = ”;
before calling $tweetout the first time.
finally i got it. this is how to get the date
$tDate = explode(“”, $endtweet[0]);
$theDate = explode(“”, $tDate[1]);
$theDate = strtotime($theDate[0]);
$theDate = date(“Y-m-d H:i:s”, $theDate);
error post above
you just put pubDate in explode