【问题标题】:PHP - API call foreachPHP - API 调用 foreach
【发布时间】:2018-07-13 17:59:49
【问题描述】:

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

  1. 我访问路线/postman-test-route
  2. 它使用一些标头和参数对 https://pro-sitemaps.com/api/ 进行 API 调用
  3. 每个 API 结果最多显示 20 个条目
  4. 当结果显示时,我迭代结果并计数条目,所以如果entries == 20,然后进行另一个API调用,但将'from'参数从0更改为20,然后30然后@987654328 @ 然后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


    【解决方案1】:

    所以您实际上是在再次调用 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 循环。

    【讨论】:

    • 看起来进程需要很长时间:消息:“系统因发生错误而停止。代码:1,消息:请求被中止,因为它超过了最大执行时间。”,跨度>
    • Localhost,我在 Google App Engine 中使用 Guzzle 托管
    • 有一个名为max_execution_time的php配置选项,默认为30秒。如果您在本地收到错误,则需要更新它。如果错误出现在 Google App Engine 上,我认为最长执行时间为 60 秒,但我不确定如何更新。你应该针对这个问题提出另一个问题,因为它不再属于这个问题的范围。
    • max_execution_time = 300 工作。谢谢@Jim,你为我做了很多,甚至为我编写了代码也解释了它是如何工作的。十分感谢! :)
    猜你喜欢
    • 2021-03-31
    • 2011-07-20
    • 2019-03-24
    • 2023-03-16
    • 2011-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多