简体   繁体   English

一次处理数百个 API 请求

[英]Handling hundreds of API requests in one time

I am trying to make a user dashboard which will generate reports of all of my 37 game accounts played by different gamers.我正在尝试制作一个用户仪表板,它将生成不同游戏玩家玩过的所有 37 个游戏帐户的报告。 so, for each i scrape data and then calculate the amount of rewards it generated.因此,对于每个我抓取数据,然后计算它产生的奖励数量。 but for that i need to do hundreds of api requests and due to max_exec_time limit of 30sec it gives me a fatal error.但为此,我需要执行数百个 api 请求,并且由于 max_exec_time 限制为 30 秒,它给了我一个致命错误。 and also I reset the time to 3000 but it completed in around 2400 (40 mins).并且我将时间重置为 3000,但它在大约 2400(40 分钟)内完成。 and i can calculate by pen-paper in 10mins of each account.我可以在每个帐户的10分钟内用笔纸计算。 so, i need a fast way to make the api calls.所以,我需要一种快速的方法来进行 api 调用。 Thanks谢谢



first it gets the cards collected by users in a week (usually they are 35-40) and then for every card it check the card's rarity and foil (gold or regular).首先它会获取用户在一周内收集的卡片(通常是 35-40 张),然后检查每张卡片的稀有度和金箔(金色或普通)。 if it is gold then save to $isgold array.如果是黄金,则保存到 $isgold 数组。 but i just checked for gold only still.但我只是检查了黄金。 there are 40 calls and in total there are 37 accounts i own.有 40 个电话,我总共有 37 个帐户。 so, when i click on get report it has to make some 1300 api calls, which doesn't seems good.所以,当我点击获取报告时,它必须进行一些 1300 次 API 调用,这似乎不太好。 is there another better way to do it...??有没有其他更好的方法来做到这一点......?

if there then please share it.如果有那么请分享它。

here is my php func这是我的 php 函数

<?php
$json = json_decode(file_get_contents("https://steemmonsters.com/cards/collection/iampolite"), 1);
    $cards = $json['cards'];
    $usercards = array();
    $isgold = array();
    
    foreach ($cards as $card) {
        if($card['player'] == 'iampolite' ) {
            $usercards[] = $card;
            
            foreach($usercards as $key=>$usercard) {
                $uid = $usercard['uid'];
                $json = json_decode(file_get_contents("https://steemmonsters.com/cards/find?ids=$uid"), 1);
                $data = $json['0']['gold'];
                if($data == true){
                    $isgold[] = $uid;
                }
            }
        }
    }
print_r(count($isgold));
echo "<br><br>";
print_r($usercards);
?>

In short:简而言之:

create a class that first looks whether the object (with the uid) is already there, for example as a Json file.创建一个类,首先查看对象(带有 uid)是否已经存在,例如作为 Json 文件。 If so, then read this.如果是这样,那么请阅读本文。 If not, then read the object from the API and save it as a Json file with the uid.如果没有,则从 API 读取对象并将其保存为带有 uid 的 Json 文件。

But you could also collect ALL objects in an array and save them in a Json file.但是您也可以收集数组中的所有对象并将它们保存在 Json 文件中。 Then you always read this Json file first and extend it with non-existent uid objects if necessary.然后你总是首先读取这个 Json 文件,并在必要时使用不存在的 uid 对象扩展它。

you have the information in your first call.您在第一次通话中就获得了信息。

https://steemmonsters.com/cards/collection/iampolite https://steemmonsters.com/cards/collection/iampolite

{
  "player": "iampolite",
  "cards": [
    {
      "player": "davemccoy",
      "uid": "G1-1-I0SED3J72O",
      "card_detail_id": 1,
      "xp": 200,
      "gold": true,
      "edition": 1,
      "market_id": null,
      "buy_price": null,
      "last_used_block": 40381744,
      "last_used_player": "iampolite",
      "last_used_date": "2020-01-30T13:14:06.000Z",
      "last_transferred_block": null,
      "last_transferred_date": null,
      "alpha_xp": null,
      "delegated_to": "iampolite",
      "delegation_tx": "e38c7919f1e4cd6537b21323e9d8efd3d500c8b9",
      "skin": null,
      "level": 4
    },
    {
      "player": "davemccoy",
      "uid": "G1-2-ICCA0EHX8G",
      "card_detail_id": 2,
      "xp": 200,
      "gold": true,
      "edition": 1,
      "market_id": null,
      "buy_price": null,
      "last_used_block": 40381744,
      "last_used_player": "iampolite",
      "last_used_date": "2020-01-30T13:14:06.000Z",
      "last_transferred_block": null,
      "last_transferred_date": null,
      "alpha_xp": null,
      "delegated_to": "iampolite",
      "delegation_tx": "e38c7919f1e4cd6537b21323e9d8efd3d500c8b9",
      "skin": null,
      "level": 4
    },
    {
      "player": "davemccoy",
      "uid": "G1-3-REN59OW45C",
      "card_detail_id": 3,
      "xp": 200,
      "gold": true,
      "edition": 1,
      "market_id": null,
      "buy_price": null,
      "last_used_block": 40380988,
      "last_used_player": "iampolite",
      "last_used_date": "2020-01-30T12:36:06.000Z",
      "last_transferred_block": null,
      "last_transferred_date": null,
      "alpha_xp": null,
      "delegated_to": "iampolite",
      "delegation_tx": "e38c7919f1e4cd6537b21323e9d8efd3d500c8b9",
      "skin": null,
      "level": 4
    },...

so you could take the information direktly:所以你可以直接获取信息:

<?php
$json = json_decode(file_get_contents("https://steemmonsters.com/cards/collection/iampolite"), 1);
    $cards = $json['cards'];
    $usercards = array();
    $isgold = array();

    foreach ($cards as $card) {
        if($card['player'] == 'iampolite' ) {
            $usercards[] = $card;

            foreach($usercards as $key=>$usercard) {
                $uid = $usercard['uid'];

                if($usercard['gold'] == true){
                    $isgold[] = $uid;
                }
            }
        }
    }
print_r(count($isgold));
echo "<br><br>";
print_r($usercards);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM