Twitter的API 1.1#标签计数 [英] Twitter API 1.1 Hashtag Count

查看:203
本文介绍了Twitter的API 1.1#标签计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题搞清楚怎么回事总数为次在Twitter上使用#标签的数量。在过去,我用下面的code这没有工作,但 HTTP://search.twitter。 COM / search.json 地址已经被由Twitter退役。旧code是:

 < PHP
   全球总$,$#标签;
   // $#标签='#supportvisitbogor2011';
   $#标签='#MyHashtag';
   $总额= 0;
   功能getTweets($ HASH_TAG,$页){
      全球总$,$#标签;
      $ URL ='http://search.twitter.com/search.json?q='.urlen$c$c($hash_tag).'&';
      。$ URL =页面='$页。
      $ CH = curl_init($网址);
      curl_setopt($ CH,CURLOPT_RETURNTRANSFER,TRUE);
      $ JSON = curl_exec($ CH);
      curl_close($ CH);
      //回声< pre>中;
      // $ json_de code = json_de code($ JSON);
      //的print_r($ json_de code-GT&;结果);      $ json_de code = json_de code($ JSON);
      $总+ =计数($ json_de code-GT&;结果);
      如果($ json_de code-GT&; next_page){
         $ TEMP =爆炸(&安培;,$ json_de code-GT&; next_page);
         $ p值=爆炸(=,$温度[0]);
         getTweets($#标签,$ P [1]);
      }
   }   getTweets($主题标记,1);   回声$总额;
?>

我知道你知道必须使用授权的Twitter应用,并有机会获得能够获取数据。我可以建立应用程序,我可以拉用下面的code数据的列表,但我不知道如何使用这些数据拿出一个总数。有人可以帮助我通过更改的code我还是帮我,我应该如何去了解它一共拿到。这里的code我有一个拉动包括hashtag数据:

 < PHP
在session_start();
require_once(twitteroauth.php); //路径twitteroauth的图书馆$#标签=MyHashtag;
$ consumerkey =MYINFOWOULDBEHERE;
$ consumersecret =MYINFOWOULDBEHERE;
$的accessToken =MYINFOWOULDBEHERE;
$ accesstokensecret =MYINFOWOULDBEHERE;功能getConnectionWithAccessToken($ cons_key,$ cons_secret,$组oauth_token,$ oauth_token_secret){
  $连接=新TwitterOAuth对象($ cons_key,$ cons_secret,$组oauth_token,$ oauth_token_secret);
  返回$连接;
}$连接= getConnectionWithAccessToken($ consumerkey,$ consumersecret,$的accessToken,$ accesstokensecret);$鸣叫= $连接 - >获得(https://api.twitter.com/1.1/search/tweets.json?q=$#标签);回声json_en code($鸣叫);
?>


解决方案

下面是Twitter的API 1.1从<一个做一个例子href=\"http://stackoverflow.com/questions/12916539/simplest-php-example-for-retrieving-user-timeline-with-twitter-api-version-1-1\">this.请记住这一点:


  

请注意,Twitter的搜索服务,并通过延伸,
  搜索API并不意味着是鸣叫的详尽源。 并非所有
  推文将被索引或通过搜索界面提供


https://dev.twitter.com/docs/api/ 1.1 /得/搜索/鸣叫


  1. Twitter的API-PHP ,并在你的服务器将其解压到文件夹

  2. 创建一个开发者帐户和应用程序

  3. 创建一个文件 twitter_search.php (在同一文件夹,你必须的 Twitter的API的PHP 的)

  4. 添加您的令牌和键 twitter_search.php

twitter_search.php:

 &LT; PHP
require_once('TwitterAPIExchange.php');/ **设置访问令牌在这里 - 见:https://dev.twitter.com/apps/ ** /
$设置=阵列(
    oauth_access_token'=&GT; YOUR_OAUTH_ACCESS_TOKEN
    oauth_access_token_secret'=&GT; YOUR_OAUTH_ACCESS_TOKEN_SECRET
    CONSUMER_KEY'=&GT; YOUR_CONSUMER_KEY
    CONSUMER_SECRET'=&GT; YOUR_CONSUMER_SECRET
);/ **注:设置GET字段前调用buildOauth(); ** /
$ URL ='https://api.twitter.com/1.1/search/tweets.json';
$ requestMethod =GET;
$#标签='twitterapi';
$ getfield命令='?Q = 23%'$#标签。'和; result_type的近=&安培; include_entities =真放大器;计数= 100';//执行请求
$叽叽喳喳=新TwitterAPIExchange($设置);
$ json_output = $ twitter-&GT; setGetfield($ getfield命令)
              - &GT; buildOauth($网址,$ requestMethod)
              - &GT; performRequest();//让我们来计算我们有多少个结果了
$ JSON = json_de code($ json_output);
$ N =计数($ json-&GT;的状态);
回声$ N;
?&GT;

使用Twitter的搜索API

<一个href=\"http://stackoverflow.com/questions/19175733/display-number-of-tweets-on-a-hashtag/19178263#19178263\">I最初发布这个例子在这里。

I am having an issue figuring out how to return a total count for the number of times a hashtag was used on Twitter. In the past I used the following code which did work but the "http://search.twitter.com/search.json" address has since been retired by Twitter. The old code was:

<?php
   global $total, $hashtag;
   //$hashtag = '#supportvisitbogor2011';
   $hashtag = '#MyHashtag';
   $total = 0;
   function getTweets($hash_tag, $page) {
      global $total, $hashtag;
      $url = 'http://search.twitter.com/search.json?q='.urlencode($hash_tag).'&';
      $url .= 'page='.$page;    
      $ch = curl_init($url);
      curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
      $json = curl_exec ($ch);
      curl_close ($ch);
      //echo "<pre>";    
      //$json_decode = json_decode($json);
      //print_r($json_decode->results);

      $json_decode = json_decode($json);        
      $total += count($json_decode->results);    
      if($json_decode->next_page){
         $temp = explode("&",$json_decode->next_page);        
         $p = explode("=",$temp[0]);                
         getTweets($hashtag,$p[1]);
      }        
   }

   getTweets($hashtag,1);

   echo $total; 
?>

I know that you know have to use a authorized twitter application and have access to be able to pull data. I was able to set up the app and I can pull a list of data using the following code but I'm not sure how to use that data to come up with a total count. Can someone help me to get a total by either changing the code I have or helping me with how I should go about it. Here's the code I have that pulls the hashtag data:

<?php
session_start();
require_once("twitteroauth.php"); //Path to twitteroauth library

$hashtag = "MyHashtag";
$consumerkey = "MYINFOWOULDBEHERE";
$consumersecret = "MYINFOWOULDBEHERE";
$accesstoken = "MYINFOWOULDBEHERE";
$accesstokensecret = "MYINFOWOULDBEHERE";

function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
  $connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
  return $connection;
}

$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);

$tweets = $connection->get("https://api.twitter.com/1.1/search/tweets.json?q=".$hashtag);

echo json_encode($tweets);
?>

解决方案

Here is one example for Twitter 1.1 API made from this. Keep this in mind:

Please note that Twitter's search service and, by extension, the Search API is not meant to be an exhaustive source of Tweets. Not all Tweets will be indexed or made available via the search interface.

https://dev.twitter.com/docs/api/1.1/get/search/tweets

  1. Download twitter-api-php and extract it to your folder at your server
  2. Create a Developer Account and an Application
  3. Create a file twitter_search.php (in the same folder where you have twitter-api-php)
  4. Add your tokens and keys to twitter_search.php

twitter_search.php:

<?php
require_once('TwitterAPIExchange.php');

/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
    'oauth_access_token' => "YOUR_OAUTH_ACCESS_TOKEN",
    'oauth_access_token_secret' => "YOUR_OAUTH_ACCESS_TOKEN_SECRET",
    'consumer_key' => "YOUR_CONSUMER_KEY",
    'consumer_secret' => "YOUR_CONSUMER_SECRET"
);

/** Note: Set the GET field BEFORE calling buildOauth(); **/
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$requestMethod = 'GET';
$hashtag = 'twitterapi';
$getfield = '?q=%23'.$hashtag.'&result_type=recent&include_entities=true&count=100';

// Perform the request
$twitter = new TwitterAPIExchange($settings);
$json_output = $twitter->setGetfield($getfield)
             ->buildOauth($url, $requestMethod)
             ->performRequest();

// Let's calculate how many results we got
$json = json_decode($json_output);
$n = count($json->statuses);
echo $n;
?>

Using the Twitter Search API

I posted originally this example in here.

这篇关于Twitter的API 1.1#标签计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆