PHP - API 调用 foreach

问题描述 投票:0回答:2

我在尝试理解为什么我的代码不起作用时遇到一些困难。它应该像这样工作:

  1. 我参观路线
    /postman-test-route
  2. 它使用一些标头和参数对
    https://pro-sitemaps.com/api/
    进行 API 调用
  3. 每个API结果最多显示20个条目
  4. 显示结果时,我会迭代结果并计算条目数,因此如果
    entries == 20
    ,则进行另一个 API 调用,但将
    'from'
    参数从
    0
    更改为
    20
    ,然后
    30
    然后
    40
    然后
    50
    ,直到条目少于
    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, // Fra enties ID, kan kjøre en foreach for hver 20 entries. Hold en counter på result, hvis mindre enn 20 så fortsett, ellers 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 30, then 40
    if($i > 20){
        getPROsitemapsEntries($i);
    }else{
        die;
    }

正如你所见,我看到了这段代码:

 if($i > 20){
            getPROsitemapsEntries($i);
        }else{
            die;
        }

我认为这会再次调用 API,并且应保存 foreach 内部的新条目(而不是覆盖)。有人能看到我做错了什么吗?我很新

谢谢!

php api foreach slim
2个回答
2
投票

因此,您实际上是在再次调用 API,只是没有迭代结果。

$shouldProcess = true;
$searchIndex = 0;
$items = [];
while ($shouldProcess) {
    $processedThisLoop = 0;
    $function_call = getPROsitemapsEntries($searchIndex);
    $responseData = json_decode($function_call->getBody(), true);

    foreach($responseData['result']['entries'] as $entries) {
        $items[] = $entries;
        $searchIndex++;
        $processedThisLoop++;
    }

    if($processedThisLoop == 0) {
        // Didn't find any so stop the loop
        $shouldProcess = false;
    }
}

var_dump($items);

在上面的代码中,我们跟踪我们在

$searchIndex
中处理的条目总数。这将使我们能够不断获得新物品而不是旧物品。

$shouldProcess
是一个
bool
,它将决定我们是否应该继续尝试从 API 获取新条目。

$items
是一个数组,将保存 API 中的所有条目。

$processedThisLoop
包含我们在此循环中处理的条目数量,即对 API 的此请求是否有任何要处理的条目?如果没有,则将
$shouldProcess
设置为 false,这将停止
while
循环。


0
投票

为了更优雅的迭代,请使用测试后循环。

do {} while ()
构造将确保 API 至少被调用一次,然后仅在没有结果或结果数不能被 20 整除时在每次迭代结束时中断。

array_push()
与展开运算符一起使用,将每批结果解压到结果数组中。很少享受的
array_push()
原生行为将返回其新计数——这样就无需在每次迭代时进行额外的
count()
调用。

代码:(模拟演示

$count = 0;
$items = [];
do {
    $response = json_decode(getPROsitemapsEntries($count)->getBody(), true);
    $count = array_push($items, ...$response['result']['entries']);
} while ($count && $count % 20 === 0);
var_export($items);

上面的代码片段是严格为每次调用返回最多 20 行的 API 调用而设计的,而不是为具有无限响应量的 API 设计的。

© www.soinside.com 2019 - 2024. All rights reserved.