PHP Function with API How Many Requests?
one text
Solution:
Pass an array storing something like [ ["type" => $type, "day" => $day] ]
instead of calling the function 5 times, since you request the same URL each time, so you can loop over that array
In example :
// Get an array as parameter
// |
// V
function apiReq($TypesAndDays){
$url2 = "http://api.example.com/../.../../";
$str2 = file_get_contents($url2);
$decode_two = json_decode($str2, TRUE);
// Loop over that array
foreach ($TypesAndDays as $TypeAndDay)
{
// set $type and $day
$type = $TypeAndDay['type'];
$day = $TypeAndDay['day'];
// the rest of your code remains unchanged
foreach($decode_two['teams'][$type][$day] as $item ) {
if (isset($item['name']['initial'])) {
echo $item['name']['initial'];
}
echo $item['name']['surname'];
echo $item['shirt'];
echo '</br>';
}
}
}
$TypesAndDays = [
[ 'type' => 'A', 'day' => 'Monday' ],
[ 'type' => 'B', 'day' => 'Tuesday' ],
[ 'type' => 'C', 'day' => 'Monday' ],
[ 'type' => 'D', 'day' => 'Tuesday' ],
[ 'type' => 'F', 'day' => 'Friday' ]
];
apiReq($TypesAndDays);
Source