只是想知道是否有人可以帮我解决以下问题。我遇到了以下代码,它们会引入最新的Twitter帖子并在网站上显示:
//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];
}
});
});
});
这是HTML 网站上的推文容器如下:
<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>
我的网站不是冷敷;因此我只想修改get-tweets.cfm并希望得到一些帮助。我遇到过以下情况:
//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.
}
所以我真的想将get-tweets.cfm重建为PHP脚本get-tweets.php;但是,我不确定我需要做什么才能让它按照coldfusion脚本工作,因为其他一切都很好?
非常感谢
答案 0 :(得分:0)
Twitter通过API提供JSON格式的推文。您需要检索这些内容,将它们缓存在您的服务器上(写出Web树之外的文件),以防Twitter经常发生故障,然后回复JSON以便Javascript接收。
我没有测试过这个,但它肯定是沿着正确的方向。见bavotasan.com
$username = "your-user-name";
$num = 5;
$feed = "http://search.twitter.com/search.json?q=from:" . $username . "&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;
我应该注意上面的解决方案旨在完全替换coldfusion脚本。要使用您已经拥有的PHP摘录,只需将缓存添加到文件和回显部分。如果不是全部那样,这应该会给你带来最大的收获。
答案 1 :(得分:0)
一个新的get_tweets脚本来获取推文,过滤到你想要的列,缓存(如果是Twitter失败的鲸鱼)和回声结果。
<?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;
?>