【问题标题】:How to get only top 5 trends with Twitter API 1.1?如何使用 Twitter API 1.1 仅获得前 5 个趋势?
【发布时间】:2017-06-12 05:53:13
【问题描述】:

我正在使用 Twitter API 了解趋势。

我当前的代码显示由 WOEID 标识的给定位置的所有趋势,例如 2295424。如何更改它以仅显示前五个趋势?

<?php
    $jsonop = $connection->get("trends/place", array('id' => '2295424'));
    //var_dump($statuses);
    foreach ($jsonop as $trend) {
        echo "As of {$trend->created_at} in ";
        foreach($trend->locations as $area)
            echo "{$area->name}";
        echo " the trends are:<br />";
        echo "<ul>";
        foreach($trend->trends as $tag)
            echo "<li>{$tag->name}</li>";
        echo "</ul>";
    }
?>

【问题讨论】:

  • 请详细解释一下。代码现在做什么?
  • 代码显示特定位置的所有趋势。用woeid 2295424给出

标签: php twitter twitter-oauth


【解决方案1】:

这并不是 Twitter 特有的。为此,您真正需要知道的是如何在 X 次迭代后跳出 PHP 循环。有多种方法可以做到这一点。一种简单的方法是跟踪计数器并在达到所需值时使用break 语句退出循环。

<?php
$jsonop = $connection->get("trends/place", array('id' => '2295424'));
//var_dump($statuses);
foreach ($jsonop as $trend) {
    echo "As of {$trend->created_at} in ";
    foreach($trend->locations as $area) {
        echo "{$area->name}";
        echo " the trends are:<br />";
        echo "<ul>";
        $counter = 0;
        foreach($trend->trends as $tag) {
            $counter++;
            echo "<li>{$tag->name}</li>";
            if ($counter == 5) break;
        }
        echo "</ul>";
    }
}
?>

【讨论】:

  • 谢谢!我添加了一些更改。
  • 哦,对了。增加计数器会很好。 :) 谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-01
  • 2016-03-29
  • 2016-12-16
  • 2014-09-17
  • 2017-03-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多