dongqiu8375 2010-08-27 07:54
浏览 25
已采纳

Twitter $获得帮助

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

  • 写回答

2条回答 默认 最新

  • doubeng1278 2010-08-27 17:14
    关注

    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;
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。