views:

58

answers:

2

Just wondering if someone can assist me with the following issue. I have come across the following code which pulls in latest Twitter posts and displays them on a site:

//Handle the scrolling of the tweets in the footer
$(function () {
    var tweetVP = $("#footerTweetsViewport");
    arrTweetNav = ECC.tweetArray();

    thisTweetID = arrTweetNav[0];

    $("ul#tweetControls > li").bind("click", function(e) {
        e.preventDefault();

        var thisPos = $.inArray(thisTweetID, arrTweetNav);

        if ($(this).hasClass("tweetPrev")) {
            nextPos = thisPos - 1;              
        } else {
            nextPos = thisPos + 1;
        }
        nextID = arrTweetNav[nextPos];

        //If that tweet exists in the DOM...
        if ($("#listOfTweets > #" + nextID).length) {
            //Reset the inactive buttons
            $("ul#tweetControls > li").removeClass("inactive");
            //Scroll to the destination
            tweetVP.scrollTo("#" + nextID, 200);
            //Set the thisID to the value of the nextID
            thisTweetID = nextID;
        }

        //Disable the controls if we're on the first or last tweet
        if (nextPos == arrTweetNav.length-1) {
            $("ul#tweetControls > li.tweetNext").addClass("inactive");
        } else if (nextPos == 0) {
            $("ul#tweetControls > li.tweetPrev").addClass("inactive");
        }
    }).bind("mousedown", function() {
        $(this).closest("li").addClass("click");
    }).bind("mouseup", function() {
        $(this).closest("li").removeClass("click")
    });

});

//Search the dom for twitter containers that need tweets loaded for
$(function() {
    $(".globalTweetWrapper").each(function() {
        var thisUsername = $(this).attr("class").replace("globalTweetWrapper ", "");
        var tweetContainer = $(this);
        var loadTweets = tweetContainer.find(".loadTweets");
        //Detect if we're going to flush the tweets
        var flushTweets = $.getUrlVar("flushTweets");
        if (flushTweets != 1) {
            flushTweets = 0;
        }

        $.getJSON("get-tweets.cfm?username=" + thisUsername + "&flushTweets=" + flushTweets, function(data) {
            if (data.length && loadTweets.length) {
                loadTweets.remove();

                $.each(data, function(i,item) {
                    if (tweetContainer.attr("id") == "listOfTweets") {
                        tweetContainer.append("<li class='tweetContainer' id='" + item.ID + "'>" + item.TWEET + "<small class='darkGray'>" + item.DATE + "</small></li>");
                    } else {
                        tweetContainer.append("<p class='tweetContainer'>" + item.TWEET + "</p><small class='darkGray'>" + item.DATE + "</small>");
                        if (i == 1) return false;
                    }
                });

                //Rebuild the tweet array
                arrTweetNav = ECC.tweetArray();
                thisTweetID = arrTweetNav[0];
            }
        });
    });
});

This is the HTML container for the Tweets on the site is as follows:

<div class="footerItem posRelative">
<h3><a href="twitter url goes here/mytwitterid" rel="external" title="Follow us on    Twitter">Follow us</a> on Twitter</h3>
<ul id="tweetControls">
<li class="tweetPrev inactive">Previous Tweet</li>
<li class="tweetNext">Next Tweet</li>
</ul>

<div id="footerTweetsViewport">
<ul id="listOfTweets" class="globalTweetWrapper">
</ul>

My site is not coldfusion; therefore I simply want to modify the get-tweets.cfm and would like some assistance please. I have come across the following:

//initialize a new curl resource
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'twitter url goes here/statuses/user_timeline/twitterusername.json?count=10');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
curl_close($ch);

if($content === FALSE) {
//Content couldn't be retrieved... Do something
} else {
//Content was retrieved do something with it.
}

So I would really like to rebuild the get-tweets.cfm to a PHP script get-tweets.php; however, I'm not sure what exactly I need this to do to get this to work as per the coldfusion script as everything else is fine?

Many thanks

A: 

Twitter provides tweets in JSON format via the API. You need to retrieve these, cache them on your server (write out to a file outside of the web tree) in case Twitter goes down as it regularly does, then echo the JSON out for the Javascript to pick up.

I haven't tested this, but its definitely along the right lines. See bavotasan.com

$username = "your-user-name";
$num = 5;

$feed = "http://search.twitter.com/search.json?q=from:" . $username . "&amp;rpp=" . $num;

$newfile = dirname(__FILE__)."/twitternew.json";
$file = dirname(__FILE__)."/twitter.json";

copy($feed, $newfile);

$oldcontent = @file_get_contents($file);
$newcontent = @file_get_contents($newfile);

if($oldcontent != $newcontent) {
    copy($newfile, $file);
}
$tweets = @file_get_contents($file);

echo $tweets;

I should note the above solution is designed to replace the coldfusion script entirely. To use the PHP excerpt you already have, just add the caching to file, and echo parts. That should get you most if not all of the way there.

danielgwood
Hi, thanks for that.So, with respect to the code you have put above, that should simply sit within the get-teets.php script...and other than that it should work with the code already displayed and place the tweets in the DOM container created in the HTML?
Bill Johnson
I believe so, though as I say I have not had opportunity to test it.
danielgwood
Will set to work on test and feedback shortly...thanks again for your help on this! Really appreciated.
Bill Johnson
Bill Johnson
p.s. I did use an actual twitterID in the php; however, I have removed it from the above for this discussion.
Bill Johnson
danielgwood
Yep, that works and thank you very much for your help....really appreciated :-) Can I add to your reputation in any way on here?
Bill Johnson
Actually, it's not just there and I wonder if I can be assisted further. looking at the tweets contained in the .cfm file it's holding them as such:[{"ID":210870045687,"TWEET":"Thoughtful enhancement from YouTube: Combine the user's desired action with the login invitation: <a href='http:\/\/bit.' rel='external nofollow'>http:\/\/bit.ly\/cRNLrP<\/a>","DATE":"2010-08-13 14:46:00"}]However, on the get-tweets.php we have I hold results as:{"results":[{"profile_image_url":"http://a0.twimg.com/profile_images/1090544552/avatar_normal.png","created_at":"Fri, 27 Aug 2010 15:04:47 +0000",
Bill Johnson
So the .cfm seems to simply collect ID, and the tweet, date and time; however, on the PHP script it pulling alot of additional data?? Any thoughts on how to replicate the .cfm results? Thanks
Bill Johnson
See new answer. Because the changes were fairly heavy, I rewrote the script from scratch. Maybe this is more what you want :)
danielgwood
Yes, that is more like it and again many thanks for that. Just don't seem to be getting anything into the DOM containers on the HTML
Bill Johnson
A: 

A new get_tweets script to get tweets, filter to the columns you wanted, cache (in case of Twitter fail whale) and echo results.

<?php

$username = "danielgwood";
$num = 5;

$feed = "http://search.twitter.com/search.json?q=from:" . $username . "&rpp=" . $num;

$cachefile = dirname(__FILE__)."/twitter.json";

// Get new contents
$newTweets = @file_get_contents($feed);

// Filter columns
$tweets = array();
if($newTweets !== null) {
    $newTweetsObj = json_decode($newTweets);

    foreach($newTweetsObj->results as $tweet) {
        $tweets[]['ID'] = $tweet->id;
        $tweets[]['TWEET'] = $tweet->text;
        $tweets[]['DATE'] = $tweet->created_at;
    }

    $newTweets = json_encode($tweets);
}

// Cache result
if($newTweets !== null) {
    @file_put_contents($cachefile, $newTweets);
}

$tweets = @file_get_contents($cachefile);

echo $tweets;

?>
danielgwood
Great help and thanks for that.
Bill Johnson