2

i'm trying to make multiple API calls at once and get data needed from each of them, how can i do it? i want to send about 100 requests at once and grab data from them.

this is the code i currently have:

<?php

$min = 7960265729;
$max = 9080098567;
$count = 0;

for($i = $min; $i <= $max; $i++) {
    try{
        $r = json_decode(file_get_contents("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=EA08B4B2B515A4BAB3D742627CDCC492&steamids=7656119$i"), true);
        $avatar = $r['response']['players'][0]['avatar'];
        $profile = $r['response']['players'][0]['profileurl'];
        if($avatar != null) {
            $count += 1;
            print_r("[$count][$i] $profile \n");
        }
    }
    catch (Throwable $t){
        continue;
    }
}

?>

this makes one api request at a time. how can i change so it can make 100?

thanks.

4
  • if you plan to use guzzle then you can send asynchronous request at the rate of 3-5 concurrently Commented Jan 3, 2021 at 17:39
  • can i make 100 requests at once with that? Commented Jan 3, 2021 at 18:10
  • though it is possible but it may lead to slowing down or deadlock(program gets hanged), so at a time 5 is recommended though it will send 5 requests then not wait for response & then send again 5 response & so on Commented Jan 4, 2021 at 6:04
  • i managed to implement curl multi in the code. i can now send 1000 or even 2000 requests at once and get data from each of them. now i'm trying to run my script in multiprocessing Commented Jan 4, 2021 at 10:53

1 Answer 1

3

If you have the curl extension installed, you can use curl_multi_exec(). What might be easier though is using a dedicated HTTP client library for this, e.g. Symfony HttpClient or Guzzle.

Please be aware that most APIs don't like it when you spam them with requests, especially if you want to iterate over some ids. If you don't abide by their rules you might get your API access revoked or get rate limited.

Sign up to request clarification or add additional context in comments.

2 Comments

it's okay. i'm aware of the limits. i tried using curl but how can i control how many api calls i want to make? or how do i even implement it in my code?
You can control the number of connections using curl_multi_setopt(). You'll probably want to use CURLMOPT_MAX_TOTAL_CONNECTIONS. HTTP client libraries will probably make things easier, especially if the API supports newer HTTP protocols, e.g. multiplexing from HTTP/2 as they will likely pick the best available option for you automatically or at least with fewer config.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.