【问题标题】:how do I retrieve the words that I tweeted most?我如何检索我发推文最多的词?
【发布时间】:2012-02-28 02:45:36
【问题描述】:

我一直在阅读 twitter 的开发者网站,但是 RESP API 中没有这样做的方法,我认为它是使用 Streaming Api,有人可以指导我如何做到这一点吗?,我想要类似的东西在 tweetstats 中,只显示推文最多的词。

感谢回复

【问题讨论】:

  • 请帮忙,拜托!!一个提示,一个建议,随便什么
  • 根据对这个问题的回复,听起来 twitter 并没有保留历史 stackoverflow.com/questions/1662151/…
  • 我知道所以我想知道一周
  • 也许从这个开始:rickyrosario.com/blog/… 然后弄清楚如何检索一周。如果您可以使用 php explode() 将推文存储到一个数组中,那么您已经成功了一半。
  • 我已经知道如何使用twitter的rest api了,让我看看

标签: php twitter


【解决方案1】:

这使用 REST API,而不是 Streaming API,但我认为它会满足您的需求。唯一的限制是它被 REST API 限制为最新的 200 条推文,因此如果您在上周有超过 200 条推文,那么它只会跟踪您最近 200 条推文中的单词。

确保将 API 调用中的用户名替换为您想要的用户名。

<?php

//Get latest tweets from twitter in XML format. 200 is the maximum amount of tweets allowed by this function.
$tweets = simplexml_load_file('https://api.twitter.com/1/statuses/user_timeline.xml?include_entities=true&include_rts=true&screen_name=kimkardashian&count=2');

//Initiate our $words array
$words = array();

//For each tweet, check if it was created within the last week, if so separate the text into an array of words and merge that array with the $words array
foreach ($tweets as $tweet) {
    if(strtotime($tweet->created_at) > strtotime('-1 week')) {
        $words = array_merge($words, explode(' ', $tweet->text));
    }
}

//Count values for each word
$word_counts = array_count_values($words);

//Sort array by values descending
arsort($word_counts);

foreach ($word_counts as $word => $count) {
    //Do whatever you'd like with the words and counts here
}

?>

【讨论】:

  • 感谢您的回答等待如果有人知道更好的方法
  • 如果您正在寻找更好的,恕我直言,您需要定义“更好”
猜你喜欢
  • 1970-01-01
  • 2012-02-22
  • 1970-01-01
  • 1970-01-01
  • 2013-12-08
  • 2019-04-23
  • 2018-05-05
  • 2018-02-15
  • 1970-01-01
相关资源
最近更新 更多