简体   繁体   English

我如何通过光标的结果遍历每个页面

[英]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文件包括:

  1. your original question example code 您的原始问题示例代码
  2. basic while-loop based solution 基本while-loop基于while-loop的解决方案
  3. the complicated Guzzle,Promise,CurlMulti approach 复杂的Guzzle,Promise,CurlMulti方法

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.

相关问题 如何遍历每个数组并调用函数? - How do I loop through array and call function for each? 如何遍历此循环分别获取每个项目 - How do I iterate through this loop Get each Item Separately 如何计算每个循环的结果? - How can I count the result of a for each loop? 如何重置结果集,以便可以再次遍历它? - How do I reset a result set so that I can loop through it again? 如何遍历多维数组以从每种颜色获得随机图像? - How do I loop through a multidimensional array to get a random image from each color? 我如何正确创建一个PHP数组以便以后遍历并获取每条信息? - How do I properly create a PHP array in order to loop through later and grab each piece of info? AJAX 如何在不重复的情况下循环遍历每个文件夹的内容? - AJAX How do I loop through the contents of each folder without repetitions? 如何使用include函数通过php变量将数据库查询的结果返回到另一个页面 - How do I return the result of a database query through a php variable to another page using the include function 如何遍历动态(jQuery)生成的texareas数组并将值发布到同一页面? - How do I loop through an array of dynamically (jQuery) generated texareas and post the values to the same page? 如何正确遍历网格? - How do I loop through grid properly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM