如何使用其API在Twitter中获取特定标签或帐户上的推文,转发和回复的总数?

时间:2012-02-16 08:57:43

标签: twitter

我要求获取特定标签或用户帐户的推文,转发和回复总数。如何有效地获得这些数字?数字应该准确,而不是100+。 我还需要获得总的直接消息。

1 个答案:

答案 0 :(得分:0)

以此网站为起点,我一直在尝试同样的事情: Pull Twitter feed into your site

<?php

require_once 'db-functions.inc.php' ; //custom database functions

function saveTweets($screen_name) {
global $link;

$screen_name = dbEscape(strtolower(trim($screen_name)));
if (!$screen_name) { echo "<p><strong>Error: No screen name declared.</strong></p>\n"; return false; }

$row = dbGetRow("SELECT `id` FROM `retweet` WHERE `screen_name`='$screen_name' ORDER BY `id` DESC LIMIT 1");
$last_id = $row['id'];


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

$affected = 0;
$twelement = new SimpleXMLElement($xml);
foreach ($twelement->status as $status) {
    $text = dbEscape(trim($status->text));
    $time = strtotime($status->created_at);
    $id = $status->id;
    $retweet_count = $status->retweet_count;
dbQuery("INSERT INTO `twit` (`id`,`screen_name`,`time`,`text`,`hidden`,`retweet_count`) VALUES ('$id','$screen_name','$time','$text','n','$retweet_count')");
     $affected = $affected + dbAffectedRows();
    }

return "<p>".number_format($affected)." new tweets from $screen_name saved.</p>\n" ;
}

echo saveTweets('stackoverflow');
echo saveTweets('Apple');
echo saveTweets('Android');
echo saveTweets('Google');

?>

<h3>Stackoverflow</h3>
<?php
require_once 'databaseconnection.php' ; //database connection function

$result = dbQuery("SELECT * FROM `retweet` WHERE `hidden` != 'y' ORDER BY `retweet_count` DESC"); 

while ($row = mysql_fetch_array($result)) {

echo $row[0];
echo "<br>";
echo date("l, M j, Y, G:i a",$row[3]); 
echo " : ";
echo stripslashes($row[2]); 
echo "<br>";
echo stripslashes($row[4]); 
echo "<br>Retweet: ";
echo stripslashes($row[6]); 
echo "<br>";
echo "<br>";
 }
?>

目前,它没有“UPDATE”条款来考虑retweet_count的增加,你可以删除:

return "<p>".number_format($affected)." new tweets from $screen_name saved.</p>\n" ;

如果您不需要查看已更新的内容。 希望有所帮助