dpqaaczn141761 2016-10-12 03:41
浏览 43

PHP如何遍历此api的页面?

So basically I am trying to get the sum of AveragePrice of every single page on this api. Right now it only gets first page the things i've tried have only gotten it to go on an endless loop crashing wamp. Heres my code for 1 page of working.

I am just really unsure how I can get it to loop through pages and get sum of every page.

<?php       
    function getRap($userId){
        $url = sprintf("https://www.roblox.com/Trade/InventoryHandler.ashx?userId=" . $userId . "&filter=0&page=1&itemsPerPage=14");
        $results = file_get_contents($url);
        $json = json_decode($results, true);

        $data = $json['data']['InventoryItems'];                    
        $rap = 0;

        foreach($data as $var) {
            $rap += $var['AveragePrice']; 
        }

        echo $rap;
    }

    $userId = 1;
    getRap($userId);
?>
  • 写回答

1条回答 默认 最新

  • dougu4448 2016-10-12 04:59
    关注

    You may get better answers by looking into the API you are working with regarding how many pages to look for. You want to loop until you hit the max pages. There should be an value in the result of your request that tells you that you've asked for a page that doesn't exist (ie. no more results). If you can get a total number of results to search for then you could do a for loop with that as your limit.

    //Change the function to accept the page number as a variable
    function getRap($userId, $i){
                $url = sprintf("https://www.roblox.com/Trade/InventoryHandler.ashx?userId=" . $userId . "&filter=0&page=" . $i . "&itemsPerPage=14");
    
    //work out how many pages it takes to include your total items
    // ceil rounds a value up to next integer.
    // ceil(20 / 14) = ceil(1.42..) == 2 ; It will return 2 and you will look for two pages
    $limit = ceil($totalItems / $itemsPerPage);
    
    // Then loop through calling the function passing the page number up to your limit.
    for ($i = 0; $i < $limit; $i++) {
        getRap($userId, $i);
    }
    

    If you cannot get the total number of items, you could loop while a fail state hasn't occured

    // look for a fail state inside your getRap()
    function getRap($userId, $i) {
        if ($result = error) { //you will have to figure out what it returns on a fail
            $tooMany = TRUE;
        }
    }
    
    for ($i = 0; $tooMany !== TRUE ; $i++) {
        getRap($userId, $i);
    }
    

    Edit: Reviewing my answer, looking for the fail state inside your function is poor form (and won't work because of the scope of the variable in this case). You could pass the variable back and forth, but I'll leave that part up to you.

    To get the total, make sure that your function doesn't print the result (echo $rap) but returns it for further use.

    Full example

    <?php       
    function getRap($userId, $i){
        $url = sprintf("https://www.roblox.com/Trade/InventoryHandler.ashx?userId=" . $userId . "&filter=0&page=" . $i . "&itemsPerPage=25");
        $results = file_get_contents($url);
        $json = json_decode($results, true);
        if ($json['msg'] == "Inventory retreived!") {
            $data = $json['data']['InventoryItems'];                    
            $rap = 0;
    
            foreach($data as $var) {
                $rap += $var['AveragePrice']; 
            }
    
            return $rap;
        } else {
            return FALSE;
        }
    }
    
    $total = 0;
    $userId = 1;
    for ($i = 0; $i < 1000 /*arbitrary limit to prevent permanent loop*/ ; $i++) {
        $result = getRap($userId, $i);
        if ($result == FALSE) {
            $pages = $i;
            break;
        } else {
            $total += getRap($userId, $i);
        }
    }
    echo "Total value of $total, across $pages pages";
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥15 帮我写一个c++工程