[英]How do i loop through each page by result of cursor
So ive been trying for a few hours now to basically get the recentAverageRap of the user selected with this api. 所以我已经尝试了几个小时来基本上获得用这个api选择的用户的recentAverageRap。 So i can get part of it with the code.
所以我可以用代码获得它的一部分。 But i can not figure out how to go to the next page if there is a next page.
但如果有下一页,我无法弄清楚如何进入下一页。 All I know is in the url i got to change: cursor= to the json value of nextPageCursor to get to next page.
所有我知道的是在我需要更改的url:cursor =到nextPageCursor的json值以进入下一页。 But ive been trying and cant get the correct info from all the pages.
但我一直在努力,无法从所有页面获得正确的信息。
Heres my code so far which only gets first page: 到目前为止我的代码只有第一页:
<?php
$userid = htmlentities($_GET['userid'], ENT_QUOTES);
$assettypes = array("Hat", "Face", "Gear", "HairAccessory", "FaceAccessory", "NeckAccessory", "ShoulderAccessory", "FrontAccessory", "BackAccessory", "WaistAccessory");
$rap2 = 0;
foreach($assettypes as $assettype){
$url = "https://inventory.roblox.com/v1/users/" . $userid . "/assets/collectibles?assetType=" . $assettype . "&sortOrder=Asc&limit=100&cursor=";
$get = file_get_contents($url);
$json = json_decode($get);
$rap = 0;
foreach($json->data as $val){
$rap += $val->recentAveragePrice;
}
$rap2 += $rap;
}
echo $rap2;
?>
Sorry to resurrect an old question, but I stumbled on this issue myself, and wrote a small PHP class to help me accomplish fetching all pages of Roblox's paginated WebAPI endpoints in a single request. 很抱歉重新提出一个旧问题,但我自己偶然发现了这个问题,并写了一个小的PHP类来帮助我在一个请求中完成获取Roblox的分页WebAPI端点的所有页面。
The basic solution (sequential & slow) would be to activate some kind of loop that continually calls the api, while incrementing ?page
and ?cursor
query param values until the response for a given AssetType no longer returns a nextPageCursor
value, then breaking the loop and moving on. 基本解决方案(顺序和慢速)将激活某种不断调用api的循环,同时递增
?page
和?cursor
查询参数值,直到给定AssetType的响应不再返回nextPageCursor
值,然后打破循环并继续前进。
<?php
function get_page($userid, $assettype, $page, $cursor){
$url = "https://inventory.roblox.com/v1/users/" . $userid . "/assets/collectibles?assetType=" . $assettype . "&sortOrder=Asc&limit=100&cursor=" . $cursor . "&page=" . $page;
$get = file_get_contents($url);
$json = json_decode($get);
$rap = 0;
$items = 0;
foreach($json->data as $val){
$rap += $val->recentAveragePrice;
$items += 1;
}
return [$rap, $items, $json->nextPageCursor];
}
function get_all_pages_for_asset_type($userid, $assettype){
$keep_fetching = true;
$cursor = '';
$page = 1;
$rap = 0;
$items = 0;
while($keep_fetching){
$results = get_page($userid, $assettype, $page, $cursor);
$rap += $results[0];
$items += $results[1];
if(!$results[2] || empty(trim($results[2]))){
// nextPageCursor empty, break
$keep_fetching = false;
break;
}else{
// nextPageCursor present, continue & fetch next page...
$page+=1;
$cursor = $results[2];
}
}
return [$page, $items, $rap];
}
$userid = htmlentities($_GET['userid'], ENT_QUOTES);
$assettypes = array("Hat", "Face", "Gear", "HairAccessory", "FaceAccessory", "NeckAccessory", "ShoulderAccessory", "FrontAccessory", "BackAccessory", "WaistAccessory");
$rap2 = 0;
$exec_time_start = microtime(true);
$items_total = 0;
$pages_total = 0;
foreach($assettypes as $assettype){
$results = get_all_pages_for_asset_type($userid, $assettype);
$pages_total += $results[0];
$items_total += $results[1];
$rap2 += $results[2];
}
echo json_encode([
'total_rap' => $rap2,
'items' => $items_total,
'pages' => $pages_total,
'rendertime' => (microtime(true) - $exec_time_start)
]);
?>
A more advanced solution (concurrent & faster) would be to fire off multiple dynamic promise chains, 1 for each AssetType, which would dynamically run in their own, individual loops until nextPageCursor
is empty, upon which that particular AssetType's promise chain could be resolved. 更高级的解决方案(并发和更快)将触发多个动态承诺链,每个AssetType 1,它们将在它们自己的单个循环中动态运行,直到
nextPageCursor
为空,然后可以解析该特定AssetType的承诺链。 Once all 10 promise chains resolve completely, (using GuzzleHttp\\Promise\\Settle
, which is like JS's Promise.all(promises[])
the final calculation is done & returned. 一旦所有10个承诺链完全解决,(使用
GuzzleHttp\\Promise\\Settle
,就像JS的Promise.all(promises[])
,最终计算完成并返回。
I've posted the full code for both solutions to Github here: https://github.com/jakedowns/roblox-api-fetch-all-pages 我已经在这里发布了两个解决方案的完整代码给Github: https : //github.com/jakedowns/roblox-api-fetch-all-pages
The index.php file includes: index.php文件包括:
while-loop
based solution while-loop
基于while-loop
的解决方案 Some basic timing comparisons between the approaches: 这些方法之间的一些基本时间比较:
// ~4 seconds for 10 pages of 607 items (incomplete)
original_example();
// ~15 seconds for 31 pages of 2483
basic_solution();
// ~4 seconds for 31 pages of 2483 items
advanced_solution();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.