Jan 31, 2013

Posted by in PHP, Technology | 13 comments

Displaying twitter feeds using PHP, ready made code to be used.

Sample twitter feed display

Sample twitter feed display

Well, it is the generic requirement that we always in a need to display twitter feeds in our website, here goes the readily available code which you can directly use in your website. Just change the twitter_id and number_of_tweets you want to display in the variable and you are all set to go.

You can also play around the css, feel free to contact me if you need any further help.

<?php
$twitter_id = 'espncricinfo';
$number_of_tweets = 4;


function linkable($text = ''){
    $text = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $text));
    $data = '';
    foreach( explode(' ', $text) as $str){
        if (preg_match('#^http?#i', trim($str)) || preg_match('#^www.?#i', trim($str))) {
            $data .= '<a href="'.$str.'">'.$str.'</a> ';
        } else {
            $data .= $str .' ';
        }
    }
    return trim($data);
}

?>
<div style="margin: auto; width:350px; border: 10px solid rgb(177, 212, 252); border-radius: 10px 10px 10px 10px;" id="twitter_lf">
    <div class="twitterSearchTitle" style="background-color: rgb(177, 212, 252); margin: 0px; padding: 0px 0px 5px; text-align: center; font-weight: bold; font-size: large; position: relative;">
        <a href="http://twitter.com/abiosystems" style="text-decoration: none; color: rgb(59, 89, 152);">
        @<?php echo $twitter_id;?> <br /> <span style="font-size:10px;">Live Twitter Feed</span></a><a href="http://www.twitter.com">
        <img src="http://cloud.github.com/downloads/malsup/twitter/tweet.gif" class="twitterSearchBird" style="width: 50px; height: 20px; position: absolute; left: -30px; top: -20px; border: medium none;">
        </a>
    </div>
   
    <div class="twitterSearchContainter" style="overflow: hidden; background-color: rgb(255, 255, 255); height: auto; display: block;">
    <?php
    //by dharam to get twitter feed
    $responseJson = file_get_contents('http://api.twitter.com/1/statuses/user_timeline.json?screen_name='.$twitter_id.'&include_rts=1&count='.$number_of_tweets);
    if($responseJson) {
        $responseAray = json_decode($responseJson);
        foreach($responseAray as $resp) {
            ?>
            <div style="padding:3px; margin:0 0 2px 0;">
            <a style="color:#00BEF2;" href="https://twitter.com/intent/user?screen_name=abiosystems" target="_blank">@<?php echo $resp->user->screen_name;?></a> <?php echo linkable($resp->text);?><br />
            <a style="color:#0E313B;" href="https://twitter.com/intent/tweet?in_reply_to=<?php echo $resp->id_str;?>">reply</a> |
            <a style="color:#0E313B;" href="https://twitter.com/intent/retweet?tweet_id=<?php echo $resp->id_str;?>">retweet</a> |
            <a style="color:#0E313B;" href="https://twitter.com/intent/favorite?tweet_id=<?php echo $resp->id_str;?>">favorite</a>
            </div>
            <?php
        }
    }
    ?>
    </div>
</div>
  1. Intazar says:

    I am junior PHP Developer. I have to develop twitter feed. I know it as you show. But i have to show date also that on which date people post on my site. How can i implement it

  2. Montassar says:

    Thank you for this nice script !

  3. Excellent. Simple and effective. Thanks

  4. it gives a warning

    Warning: file_get_contents(https://api.twitter.com/1/statuses/user_timeline.json?screen_name=espncricinfo&include_rts=1&count=4) [function.file-get-contents]: failed to open stream: No error in D:\wamp\www\owncms\tweet.php on line 31

  5. Hi,

    Can you please update the script to new twitter api 1.1, thise version not working anymore.

    Best regards

  6. Hi Dharam, it seems the code has stopped working ? it was working perfectly for a while and then suddenly stopped , without any change being made to make it stop.
    Any ideas 🙂

    KeithR

  7. Hi, I am having trouble with the new API 1.1 :s

    Can you help us please

    Thank you 🙂

  8. hi..
    {“errors”: [{“message”: “The Twitter REST API v1 is no longer active. Please migrate to API v1.1. https://dev.twitter.com/docs/api/1.1/overview.”, “code”: 68}]}

    Twiiter Api changed plz upgrade Api code..!!!!

  9. Bharath says:

    Hi Dharam

    Warning: file_get_contents(http://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=thankyousachin&include_rts=1&count=4): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /var/www/index.php on line 31

    Still same error do something

  10. it is not working for me.

  11. please check it shows just username only, there is not shown any feed..Any recommendation please tell me

  12. Thanks sir,
    It is not working for ma. Bellow errow accord.please help me sir.

    Warning:
    file_get_contents(http://api.twitter.com/1/statuses/user_timeline.json?screen_name=scoopwhoopwews&include_rts=1&count=4): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in.

Leave a Reply