简体   繁体   English

从Twitter PHP获取特定主题标签的计数

[英]getting the count of a specific hashtag from twitter PHP

i am able to get all twitter posts of a specific hashtag in PHP. 我能够在PHP中获取特定主题标签的所有twitter帖子。 What now is that i want to get the total number of count for that particular hashtag. 现在,我想获取该特定主题标签的总数。 i am using curl , SimpleXMLElement as well as twitter API search calls. 我正在使用curl,SimpleXMLElement以及twitter API搜索调用。 Any idea on how to get the total number of count for that specific hashtag? 关于如何获取该特定主题标签的总数的任何想法吗?

<?php
$pagetitle = "Pull Twitter Hashtags";


function getTweets($hash_tag) {

    $url = 'http://search.twitter.com/search.atom?q='.urlencode($hash_tag).'&result_type=recent' ;
    echo "<p>Connecting to <strong>$url</strong> ...</p>";
    $ch = curl_init($url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $xml = curl_exec ($ch);
    curl_close ($ch);

//If you want to see the response from Twitter, uncomment this next part out:
//echo "<p>Response:</p>";
//echo "<pre>".htmlspecialchars($xml)."</pre>";

    $affected = 0;
    $twelement = new SimpleXMLElement($xml);
    foreach ($twelement->entry as $entry) {
    $text = trim($entry->title);
    $author = trim($entry->author->name);
    $time = strtotime($entry->published);
    $id = $entry->id;

    //echo count($entry->text);
    echo "<p>Tweet from ".$author.": <strong>".$text."</strong>  <em>Posted ".date('n/j/y g:i a',$time)."</em></p>";
}



return true ;
}

getTweets('#converse');


?>

使用count()

$number_of_tweets = count($twelement->entry);

There's no such thing as "all tweets" - only "all tweets in the past x hours" (maybe days). 没有“所有推文”之类的东西,只有“过去x小时内的所有推文”(也许是几天)。

That's the first thing any Twitter developer should know - consider tweets an infinite stream. 这是任何Twitter开发人员都应该知道的第一件事-考虑推文无限流。 You cannot count an infinite stream, nor can you get all of them. 您无法计算出无限的流,也无法获取所有的无限流。

To count "all tweets about #hashtag in the past hour" simply get all tweets with that hashtag from the past hour, then count them. 要计算“过去一个小时内所有与#hashtag有关的推文”,只需获取过去一个小时内所有带有该hashtag的推文,然后对其进行计数即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM