where's the salt?

04/19
2010

Add a Twitter Feed with PHP (201,185 views)

Add a twitter feed to your website

UPDATE 10-17-2012: change in Twitter API!

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;
    // UPDATE 10-17-2012: change in Twitter API: replace next line with this code:
    $feed = 'https://api.twitter.com/1/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 (7 years, 0 months ago.)
Posted in: business mix, tech mix
Comments RSS Feed Comments RSS Feed
Reply
Ttrackback
About the author:

Nina Khoury is a software engineer, self-described geek and EVP of ninanet site solutions. She founded one of the first online agencies in Austria in 1997, taught at various universities for seven years and now lives in Sin City - Las Vegas, NV.

26 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
  20. 20

    Thanks for this great script!

    BlackHat Spot SEO (@BlackHatSpot) on July 23rd, 2012 at 12:58 am
  21. 21

    Same problem as one of the posters above, it worked for a 20 minutes or so them it wont display anything when i refresh. Is twitter blocking this for some reason? You can see the problems here – http://www.shagrr.com

    realshagrr on August 14th, 2012 at 9:44 am
  22. 22

    Twitter does have a limit on requests per hour/IP Address. That might be the problem…

    nina on August 14th, 2012 at 2:49 pm
  23. 23

    I’ll be attempting to add a simple check to make sure twitter responds… and when it doesn’t, provide an “fail guppy” message. Wish me luck!

    Jeff on November 29th, 2012 at 11:21 am
  24. 24

    Good luck!

    Nina on November 29th, 2012 at 6:26 pm
  25. 25

    Thank you so much. I have been trying to find a simple solution to this and this works perfect. I suck at PHP so I’ll have to learn a bit more to figure out how to display it out like I want, but this is fantastic!

    Spastek on December 10th, 2012 at 11:39 pm
  26. 26

    Glad I was able to help!

    Nina on December 11th, 2012 at 10:24 am

Leave a Reply


search

Categories

Send this to friend