where's the salt?

04/19
2010

Add a Twitter Feed with PHP (72,501 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;
}
(Visited 15,786 times, 1 visits today)

Tags:

Date posted: Monday, April 19th, 2010 at 8:24 am (14 years, 8 months ago.)
Posted in: business mix, tech mix
Comments RSS Feed Comments RSS Feed
Reply
Ttrackback

19 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
  3. 3

    A very simple solution to a genuine problem Ive been having.

    Thanks for your help

    My Shadow Self on November 4th, 2010 at 4:11 am
  4. 4

    Great little script, now using it on our site. Thanks a lot.

    Chris Owen on April 1st, 2011 at 1:46 pm
  5. 5

    Can you update this to include the tweet date and time?

    Sean on April 16th, 2011 at 5:31 pm
  6. 6

    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!

    kid2digit on April 27th, 2011 at 1:31 pm
  7. 7

    This worked great the first time I used it. But after refreshing, it displays nothing now. Anyone else run into this?

    Paul on August 2nd, 2011 at 9:45 am
  8. 8

    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.

    Eric on August 12th, 2011 at 4:22 pm
  9. 9

    Many thanks for the script, and special thanks to Eric, with his tip, i managed to display french charcters correctly.

    Danny Gates on October 12th, 2011 at 3:50 am
  10. 10

    if im using this with wordpress where do I put the php code?

    nj on October 17th, 2011 at 3:42 pm
  11. 11

    Thanks for sharing, works a treat on the mesh band website… http://www.themeshband.co.uk . Good work!

    Mesh Wedding Band on December 23rd, 2011 at 10:28 am
  12. 12

    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

    ble on March 5th, 2012 at 9:28 pm
  13. 13

    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;

    nina on March 6th, 2012 at 9:35 am
  14. 14

    My php is so so. Could you update the post with a an alternate version which has the time stamp in it?

    Ethan Hackett on March 12th, 2012 at 4:40 pm
  15. 15

    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

    nina on March 13th, 2012 at 8:37 am
  16. 16

    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

    john on March 14th, 2012 at 7:16 am
  17. 17

    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.

    nina on April 4th, 2012 at 9:28 am
  18. 18

    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);

    ble on April 3rd, 2012 at 9:27 pm
  19. 19

    error post above
    you just put pubDate in explode

    ble on April 3rd, 2012 at 9:29 pm

Leave a Reply


search

Categories

css.php