34 responses to “Integrate Twitter Into Your Web Site Using PHP”

  1. Jesse

    Awesome work.. this is exactly what I needed. Also here’s some additional code for anyone wanting to output a bit more:

    <?php foreach ($feed as $item): ?>
       <h2><a href="<?=$item['link']?>"><?=date('F j, Y - g:ia', strtotime($item['date']))?></a></h2>
    <?php endforeach; ?>

    Basically I cleaned up the date output with the strtotime() function. To format the date to your liking you should refer to PHP date() formats

    Great script man… would be great to expand this a bit more for less savvy php users.

    Is it just me or is the Title and Desc in the RSS the same data?

  2. flapane

    Any way to include replies?

  3. flapane

    FYI I’ve just made it building a Yahoo pipe from scratch, see the result in my homepage (lower right corner, LAST TWEETS), and tell me what do you think.

  4. Markus Steiger

    Thanks !!
    I’m use this with “jQuery .load()”
    Idea …

  5. Just Web Design

    Just what I was looking for! Worked a treat!
    Also altered it slightly to bring in our Tumblr blog from the RSS feed.

  6. Peter

    This is awesome, been browsing for days trying to find something that actually works.

    Just one question based on Jesse’s comment, any idea how to output the time as “time ago” rather than just output the exact date from the rss file?

  7. Peter

    Thanks for the reply Dimas,

    Seems that it keeps outputing 705 days for all tweets :S not sure where i’ve gone wrong.

    I’m not really that experienced in PHP, seems pretty straight forward though when using human_time_diff on comments or posts from within WP but i can’t seem to figure out how to implement it into your twitter code.

    Any help?

  8. Peter

    It’s actually outputing the following: “14839 days ago” for each tweet.

    I modified the date in the array as you suggested above and then used the following to print the time:

     echo human_time_diff(('date'), current_time('timestamp')) . ' ago';
  9. Peter

    That did the trick! Thanks again Dimas.

  10. Browntree Design

    Nice, worked well used it to pull other rss feeds too thanks.

  11. Dee

    thanks for the code, exactly what I needed!

  12. Mark

    Thanks for this post Dimas, I just set it up myself and it works beautifully!

  13. Bastian

    This code is just what I was looking for. But it seems like Twitter doesn’t allow RSS-Feeds anymore. Do you know an alternative way?

  14. Pendo

    I get the following errors —
    Warning: file_put_contents(tmp-1315951485.xml): failed to open stream: Permission denied in C:\web\includes\twitter.php on line 26 Warning: file_put_contents(tmp-info.txt): failed to open stream: Permission denied in C:\web\includes\twitter.php on line 42

    However, the tweets come out ok. I’m assuming that means it can’t create a cache file. I’m on win 2008 IIS7. I’ve used php cgi-wrap for linux in the past to get around permissions issues….Not sure what to do with Windows…. Thanks for the great tutorial!

  15. Edd

    Is there a way of using php to integrate a specific list I’ve got set up on my twitter account so that users on my website will only view tweets from accounts in a specific list?

  16. Brent


    I’ve modified it slightly but it is working great. How would you go about cleaning up old cache files? I think you really only need to store the one cache file, right?

  17. crashfellow

    Hoping you have a solution. If you delete the cache files, it will spit an error msg. You then refresh and the tweets show up. The cache files also do exist at that point.

    So i think there’s a bug in the script where the first time it runs, it has to create the files, but creates them after it loads. So because of this it errors out.

    Any solution for this??

  18. Brent

    Has the solution changed since my comment? If not, it hasn’t been cleaning up for me. Perhaps I implemented it incorrectly …

  19. Brandon Bowman

    I get this error. I wonder is it a server config issue?
    Warning: file_get_contents(tmp-1329342210.xml) [function.file-get-contents]: failed to open stream: No such file or directory in ../twitter/twitter-feed.php on line 44

    Warning: DOMDocument::loadXML() [domdocument.loadxml]: Empty string supplied as input in ..//twitter/twitter-feed.php on line 50

  20. Dave

    I get

    Warning: curl_setopt(): supplied argument is not a valid cURL handle resource in ….. on line 16(17, 18 and 19)

    any ideas as to why?

  21. Curt

    Thanks for the code. It’s the best I saw in my search. I was able to enhance it to filter what posts I wanted to be seen.

  22. Mike Silva

    I am not a PHP expert, but was able to make this work. My next question would be how can I make this work for multiple twitter accounts.

    For example on my website I would like for there to display several twitter accounts.


  23. Shandy

    Quality script, thanks.

    Since Twitter have recently made a lot of changes to api and feeds ive had to update the url to –


    Im having trouble with one thing though, my host providers server time does not change for daylight saving time (DST), so the server clock is an hour off. Because of this my feed is not updated very often regardless of the cache time, it updates once or twice a day. Any advice on altering the code to take this in to account?

  24. Jaimin Vaja

    thanks a lot,This is very helpful!
    I have also integrated Twitter for suvi.me iPhone application in PHP CodeIgniter in very simple way.

  25. Peter

    it work.. hurray.. thanks

Leave a Reply