如何使用 Twitter API 1.1 只获得前 5 个趋势?
How to get only top 5 trends with Twitter API 1.1?
我正在使用 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>";
}
?>
这并不是 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>";
}
}
?>
我正在使用 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>";
}
?>
这并不是 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>";
}
}
?>