我制作了一个使用 Twitch API 的小脚本。API 只允许每个查询最多 100 个结果。我希望继续进行此查询,直到没有更多结果为止。
我背后的理论是运行一个 foreach 或 while 循环并每次将偏移量增加 1。
然而,我的问题是我无法更改其自身的 foreach 参数。有没有办法有效地执行这个而不导致无限循环?
这是我当前的代码:
<?php
$newcurrentFollower = 0;
$offset=0;
$i = 100;
$json = json_decode(file_get_contents("https://api.twitch.tv/kraken/channels/greatbritishbg/follows?limit=25&offset=".$offset));
foreach ($json->follows as $follow)
{
echo $follow->user->name . ' (' . $newcurrentFollower . ')' . "<br>";
$newcurrentFollower++;
$offset++;
$json = json_decode(file_get_contents("https://api.twitch.tv/kraken/channels/greatbritishbg/follows?limit=25&offset=".$offset));
}
?>
使用 While 循环:
while($i < $total)
{
$json = json_decode(file_get_contents("https://api.twitch.tv/kraken/channels/greatbritishbg/follows?limit=25&offset=".$offset));
echo $json->follows->user->name . ' (' . $newcurrentFollower . ')' . "<br>";
$newcurrentFollower++;
$offset++;
$i++;
}
最终回应了这一点(没有成功抓住任何名字):
这是 $json->follows 的 API 部分: