【问题标题】:Twitter Search API - Popular tweets not workingTwitter 搜索 API - 热门推文不起作用
【发布时间】:2016-01-18 02:13:45
【问题描述】:

我在使用 Twitter 搜索 API 时遇到问题。对于用户给出的主题,我正在尝试获取 100 条最受欢迎的推文的转发总数。

我正在尝试用这段代码来实现这一点。

<?php
  //Debugging
  ini_set('display_errors', 'On');
  error_reporting(E_ALL);

  //Include Tokens file
  require_once('tokens.php');
  //Include Auth lib
  require_once('twitterAuth/TwitterAPIExchange.php');

  //Grab subject from url
  $subject = $_GET["subject"];

  //Request
  $url = 'https://api.twitter.com/1.1/search/tweets.json';
  $getfield = '?q='.$subject.'&count=100&result_type=popular';
  $requestMethod = 'GET';
  $twitter = new TwitterAPIExchange($settings);
  $result = $twitter->setGetfield($getfield)
          ->buildOauth($url, $requestMethod)
          ->performRequest();

        $json_output = json_decode($result, true); //getting the file content as array
        $count = 0;
        $total = 0;
        foreach($json_output['statuses'] as $tweets) {
            $count = intval($tweets['retweet_count']);
            $total = $total + $count;
        }
        echo '{"data":[{"retweet_count":'.$total.'}]}';
?>

虽然当我使用 result_type=mixedresult_type=recent 时此代码运行良好,但由于某种原因,当我将其更改为 result_type=popular,尽管 Twitter API 已在其文档中列为一个选项。 Twitter Search API documentation 。当我运行请求时,它不会返回任何错误,也不会显示任何反馈。当我用混合或最近替换流行时,代码效果很好。被困在这个问题上一段时间了,非常感谢您的帮助!提前致谢!

【问题讨论】:

    标签: php api twitter


    【解决方案1】:

    我认为这在很大程度上取决于您搜索的内容。 Twitter 搜索 API 仅允许您检索过去一周的推文。如果那一周没有热门推文,您将看不到任何结果。

    例如 - 搜索“Android”会显示这些热门结果

    https://snap.apigee.com/1KlHi9m

    但如果我搜索“qwertyuiop”,则 API 没有结果

    https://snap.apigee.com/1ZT56wV

    即使正常的 Twitter 搜索显示结果 https://twitter.com/search?q=qwertyuiop&src=typd

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-05
      • 1970-01-01
      • 2015-12-10
      • 1970-01-01
      • 2013-05-24
      • 2018-09-15
      • 2012-09-26
      • 2013-08-19
      相关资源
      最近更新 更多