通过 php 拉取推文,通过 JQuery/Ajax 更新
我目前有一个脚本,可以从 Twitter 中提取推文(关于搜索主题,例如“很棒”)并显示它们。该脚本显示最新的 20 条推文。
我想进一步开发我的脚本。我想检查页面加载后是否发布了任何新推文,如果有则显示它们,同时每页仍仅显示 20 条(因此它们都向下移动)。
这是目前我所拥有的:
<?php
function get_file($uri) {
return file_get_contents($uri);
};
$xml = get_file('http://search.twitter.com/search.atom?q=awesome%20-rt&lang=en&rpp=20');
$tweets = new simpleXMLElement($xml);
?>
<div id="entries">
<?php
foreach ($tweets->entry as $tweet) {
echo '<div class="entry">';
echo '<img class="tweet-pic" src="'.$tweet->link[1]->attributes()->href.'" />';
echo '<p class="tweet">'.$tweet->title.'</p>';
echo '<p class="tweep"><a class="link" href="'.$tweet->link[0]->attributes()->href.'">'.$tweet->author->name.'</a></p>';
echo '<div class="clear"></div>';
echo '</div>';
echo '<hr/>';
}
?>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js" language="javascript"></script>
<script>
$(document).ready(function(){
getlatest();
});
function getlatest() {
$.ajax({
type: "GET",
url: "index.php",
cache: false,
success: function(html){
$("div#entries").prepend(html);
$(".entry").slideDown("1000");
}
});
setTimeout("getlatest();",10000);
}
</script>
任何帮助将不胜感激。
I currently have a script that pulls tweets (on a search topic e.g. 'awesome') from twitter, and displays them. The script displays the latest 20 tweets.
I want to develop my script further. I would like to check to see if any new tweets have been tweeted after the page has loaded, if so then show them, whilst still only showing 20 per page (so they all move down).
Here is currently what I have so far:
<?php
function get_file($uri) {
return file_get_contents($uri);
};
$xml = get_file('http://search.twitter.com/search.atom?q=awesome%20-rt&lang=en&rpp=20');
$tweets = new simpleXMLElement($xml);
?>
<div id="entries">
<?php
foreach ($tweets->entry as $tweet) {
echo '<div class="entry">';
echo '<img class="tweet-pic" src="'.$tweet->link[1]->attributes()->href.'" />';
echo '<p class="tweet">'.$tweet->title.'</p>';
echo '<p class="tweep"><a class="link" href="'.$tweet->link[0]->attributes()->href.'">'.$tweet->author->name.'</a></p>';
echo '<div class="clear"></div>';
echo '</div>';
echo '<hr/>';
}
?>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js" language="javascript"></script>
<script>
$(document).ready(function(){
getlatest();
});
function getlatest() {
$.ajax({
type: "GET",
url: "index.php",
cache: false,
success: function(html){
$("div#entries").prepend(html);
$(".entry").slideDown("1000");
}
});
setTimeout("getlatest();",10000);
}
</script>
Any help would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,当您获取 XML 时,每条推文都有一个关联的时间戳。从这里开始,在您的 AJAX 调用中(您可以使用 setTimeout 每 X 次运行一次,您可以比较列表中的第一条推文和 XML 中的第一条推文
如果它的日期较新,您应该:
第一个。我的意思是,第一个来自
XML 不一定是独一无二的新事物
进入通话。
Well, when you get the XML each tweet has a timestamp associated. From here, in your AJAX call (which you could run every X time with setTimeout, you can compare the first tweet in your list and the first from the XML.
If it's date is more recent, you should:
first one. I mean, the first from the
XML doesn't have to be the unique new
into the call.