告诉推文是否转发?

时间:2012-01-17 22:30:45

标签: api twitter

我正在使用Twitter API来从特定用户那里获取推文。我完全按照自己的意愿工作,除了能够判断某个特定的推文是否来自用户是原创还是转推。

我正在使用以下电话: https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=philiprucker&count=1

在查看结果时,似乎我应该能够从结果中提取retweeted,它应该返回true或false。但是,这只返回字符串retweet

$url = "http://api.twitter.com/1/statuses/user_timeline.xml?include_entities=true&include_rts=true&screen_name=$screen_name&count=200" ;
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
$xml = curl_exec ($ch);
curl_close ($ch);

$twelement = new SimpleXMLElement($xml);
foreach ($twelement->status as $status) {
    $text = dbEscape(trim($status->text));
    $time = strtotime($status->created_at);
    $id = $status->id;
    $num_retweets = $status->retweet_count;
    $retweet = $status->retweeted;
    dbQuery("INSERT INTO `twitter` (`id`,`screen_name`,`time`,`text`,`hidden`, `numRetweets`, `retweet`) VALUES ('$id','$screen_name','$time','$text','n','$num_retweets','retweet')");
    // dbQuery("INSERT INTO `twitter` (`id`,`screen_name`,`time`,`text`,`hidden`) VALUES ('$id','$screen_name','$time','$text','n')");
}

这是我所描述的代码。我相信所有相关的代码都在那里。任何帮助将不胜感激!

4 个答案:

答案 0 :(得分:10)

您专门将字符串retweet放入表中,而不是$retweet的值。

答案 1 :(得分:6)

快速澄清问题:您是否希望存储推文是否被转发,或者您是否希望存储发出请求的用户是否转发了推文?

附加到状态的“转推”布尔字段对于发出请求的用户来说是透视的 - 它表示当前用户是否转发了推文,而不是推文是否被转发。非零retweet_count可以更好地指示推文是否被转发。

答案 2 :(得分:2)

使用简单的GET请求和Yahoo Query Language (aka YQL)访问retweeted节点可以像这样实现:

jsFiddle DEMO: YQL Rest Query for Twitter Retweeted Status via JSON

回调json结果如下所示:

cbfunc({
 "query": {
  "count": 1,
  "created": "2012-12-31T09:51:58Z",
  "lang": "en-US",
  "results": {
   "json": {
    "retweeted": "false"
   }
  }
 }
});

你也可以将其作为xml返回:

<?xml version="1.0" encoding="UTF-8"?>
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng"
    yahoo:count="1" yahoo:created="2012-12-31T09:54:49Z" yahoo:lang="en-US">
    <results>
        <json>
            <retweeted>false</retweeted>
        </json>
    </results>
</query>

以下是使用以太网 true 返回 转发 节点的YQL Statement的样子或 false 作为结果:

SELECT retweeted FROM json WHERE url="https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=philiprucker&count=1"

这个简单的GET请求只是ajax();请求,其成功函数如this

// The YQL Statement used below is shown next, starting with the word SELECT:
//
// SELECT retweeted FROM json WHERE url="https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=philiprucker&count=1"
//
// View the above YQL Statement using Yahoo Console at: 
// http://developer.yahoo.com/yql/console/?q=SELECT%20retweeted%20FROM%20json%20WHERE%20url%3D%22https%3A%2F%2Fapi.twitter.com%2F1%2Fstatuses%2Fuser_timeline.json%3Finclude_entities%3Dtrue%26include_rts%3Dtrue%26screen_name%3Dphiliprucker%26count%3D1%22&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys
//
// The variable 'q' below is the Yahoo Rest Query. That is provided after creating the above Yahoo Statement.
// It's provided at the bottom of that web page.

var q = 'http://query.yahooapis.com/v1/public/yql?q=SELECT%20retweeted%20FROM%20json%20WHERE%20url%3D%22https%3A%2F%2Fapi.twitter.com%2F1%2Fstatuses%2Fuser_timeline.json%3Finclude_entities%3Dtrue%26include_rts%3Dtrue%26screen_name%3Dphiliprucker%26count%3D1%22&format=json';

// Use simple jQuery .ajax along with the above YQL Rest Statement.
// The benefit of YQL rest statement is we can select the desired node, in this case "retweeted"
$.ajax({
    url: q,
    dataType: "json",
    success: function(data) {

        // Enable to show the jQuery data Object received in the browsers console.
        //console.log(data);

        // If we have data, continue.
        if (data) {

            // Display retweeted value of 'true' or 'false' via browser alert.
            alert('The retweeted status is: ' +  data.query.results.json.retweeted );

        }

    }
});

编辑2:检索全球转回的价值。

Reference: OP's Tweet with Viewable Retweet Count

jsFiddle DEMO: YQL Rest Query for Twitter Global Retweeted Status via HTML

这一次,该推文的 总转发全球统计 将直接从Twitter页面通过推文进行数据报废。此方法不需要访问原始推文,因为任何转推都会显示此全局计数。

要验证jsFiddle是否显示实际转发的计数,请使用上面提供的链接引用OP的推文页面。

此过程与上述过程类似,但这次.ajax() dataTypehtml且YQL语句如下:

SELECT * FROM html WHERE url="https://twitter.com/PostBaron/status/286544211556319233" AND xpath="//a[@class='request-retweeted-popup']"

请注意上述YQL Statement XPATH也用于访问所需的特定数据。这样可以防止下载整个HTML网页,并且只访问 Global Tweet Count 的特定数据。这是可能的,因为该值位于具有唯一 classname request-retweeted-popup的网页锚点中。为了快速发现该类名,我使用了 Inspect Element 工具,当我在网页上悬停该值时浏览器具有该工具。知道了类名后,我对响应进行了微调,以便返回<strong>标记中的值,这是我们想要知道的数字。

当然,0的值意味着推文从未被转发过,但更重要的是表明这是原始的推文本身。

最终结果是,您的数据不需要Twitter API,因为您需要抓取实际的公共网页才能获得全球推文计数

$.ajax({
    url: q,
    dataType: "html",
    success: function(data) {

        // If we have data, continue.
        if ( $(data).find('.request-retweeted-popup') ) {

            // Store the value of total retweets in this variable.
            var totalRetweets = $(data).find('strong').text();

            // Show browser alert of the totalRetweets value. Change alert() to console.log() for console log browser responses.
            alert('The following Tweet has been retweeted: ' + totalRetweets +  ' times');

        }

    }
});

答案 3 :(得分:1)

如果有人还在读这篇文章,我想也许这个帖子可以更简洁,更正确的当前api(1.1)https://stackoverflow.com/a/24041978/1449799

TL;博士 检查状态对象上是否存在retweeted_status。