Arpit Patidar
Arpit Patidar

Reputation: 69

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. 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. and also I reset the time to 3000 but it completed in around 2400 (40 mins). and i can calculate by pen-paper in 10mins of each account. so, i need a fast way to make the api calls. 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). if it is gold then save to $isgold array. but i just checked for gold only still. there are 40 calls and in total there are 37 accounts i own. so, when i click on get report it has to make some 1300 api calls, which doesn't seems good. is there another better way to do it...??

if there then please share it.

here is my php func

<?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);
?>

Upvotes: 0

Views: 167

Answers (2)

db1975
db1975

Reputation: 775

In short:

create a class that first looks whether the object (with the uid) is already there, for example as a Json file. If so, then read this. If not, then read the object from the API and save it as a Json file with the uid.

But you could also collect ALL objects in an array and save them in a Json file. Then you always read this Json file first and extend it with non-existent uid objects if necessary.

Upvotes: 1

db1975
db1975

Reputation: 775

you have the information in your first call.

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);
?>

Upvotes: 0

Related Questions