如何使用Twitter API仅针对每个请求获取新推文?

时间:2013-05-18 13:48:12

标签: php ajax jquery twitter

我正在尝试从Twitter API获取包含一些#标签的新推文。我感兴趣的是每次请求结果时新推文的数量。像这样:

10:20 AM: 100 tweets containing #google 
10:22 AM: 130 tweets containing #google

但是现在不知怎的,我的结果保持不变。这是我的代码:

PHP(tweets.php):

<?php 

$json = file_get_contents('http://search.twitter.com/search.json?q=%23apple:)&include_entities=true&rpp=100', true);
echo $json;
?> 

Javascript:

function getTweets() {
    var tweets = 0;
    $.ajax({
        url: "tweets.php",
        crossDomain: true,
        dataType: 'JSON'
        }).done(function(data) {
            $.each( data.results, function( key, value ) {
                console.log(value.entities);
                tweets++;

            });

    });
}
$(document).ready(function() {

    setInterval("getTweets()", 5000);

});

我怎样才能获得更新?

编辑:我的代码有效,但结果并非真正的更新。他们一遍又一遍地得到相同的结果。

2 个答案:

答案 0 :(得分:1)

$(document).ready(function() {

    setInterval(function(){ getTweets();}, 5000);

});

答案 1 :(得分:0)

file_get_contents()会返回字符串数据类型,而非 JSON 。看看是否有效:

$json = json_encode(file_get_contents('http://search.twitter.com/search.json?q=%23apple:)&include_entities=true&rpp=100', true));