- Я посещаю маршрут /postman-test-route
- Он выполняет вызов API к https://pro-sitemaps.com/api/ с некоторыми заголовками и параметрами.
- Каждый результат API показывает максимум 20 -entries
- Когда результат отображается, я перебираю его и подсчитываю записи. Если записи == 20, то выполняется еще один вызов API, но параметры 'from' изменяются с 0 на 20, затем 40, затем 60< /code> и т. д., пока количество записей не станет МЕНЬШЕ 20.
Код: Выделить всё
$app->map(['GET', 'POST'],'/postman-test-route', function (Request $request, Response $response) {
function getPROsitemapsEntries($total_from)
{
$client = new Client([
'sink' => 'C:\Users\****\Desktop\temp.txt'
]);
$r = $client->request('POST', 'https://pro-sitemaps.com/api/', [
'form_params' => [
'api_key' => 'ps_UmTvDUda.***************',
'method' => 'site_history',
'site_id' => 3845****,
'from' => $total_from, // From the entity's ID, can run a foreach for every 20 entries. Keep a counter on result, if less than 20 then continue, otherwise die.
]
]);
return $r;
}
$function_call = getPROsitemapsEntries(0);
$responseData = json_decode($function_call->getBody(), true);
$i = 0;
$items = array(); // ALL entries should be saved here.
foreach ($responseData['result']['entries'] as $entries) {
$items[] = $entries;
$i++;
}
// Here it should call the API again with 'from' = 20, then 40, then 60
if ($i > 20) {
getPROsitemapsEntries($i);
} else {
die;
}
Код: Выделить всё
if ($i > 20) {
getPROsitemapsEntries($i);
} else {
die;
}
Подробнее здесь: https://stackoverflow.com/questions/513 ... -collected