Reputation: 43
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.
Upvotes: 2
Views: 2104
Reputation: 17166
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.
Upvotes: 3