【问题标题】:Push Request from a Do while using Guzzle使用 Guzzle 时从 Do 推送请求
【发布时间】:2020-10-08 13:42:08
【问题描述】:

我的结果数组有问题,我最初想要的是这样的

$promises = [
    '0'  => $client->getAsync("www.api.com/opportunities?api=key&page=1fields=['fields']"),
    '1'  => $client->getAsync("www.api.com/opportunities?api=key&page=2fields=['fields']"),
    '2'  => $client->getAsync("www.api.com/opportunities?api=key&page=3fields=['fields']")
];

一组请求承诺,我将使用它,因为我想从我正在使用的 API 中检索数据集合。这是 API 第一页的样子

在我的请求中,我想获得第 2、3、4 页。 这是第 2 页的样子

我在我的 PHP 脚本上做了一个 do while 循环,但它似乎运行了一个无限循环 这就是它应该如何工作的方式。首先,我运行初始请求,然后获取 totalRecordCount = 154 并将其减去 recordCount = 100 如果差异为 != 0 它再次运行它并更改 $pageNumber 并将其推送到承诺。

这是我的功能代码。这是我的代码



function runRequest(){

    $promises = [];
    $client = new \GuzzleHttp\Client();
    $pageCounter = 1;
    $globalCount = 0;

   do {
        //request first and then check if has difference
        $url = 'https://api.com/opportunities_dates?key='.$GLOBALS['API_KEY'].'&page='.$pageCounter.'&fields=["fields"]';

        $initialRequest = $client->getAsync($url);

        $initialRequest->then(function ($response) {
            return $response;
        });

        $initialResponse = $initialRequest->wait();
        $initialBody = json_decode($initialResponse->getBody());

        $totalRecordCount = $initialBody->totalRecordCount;//154 

        $recordCount = $initialBody->recordCount;//100  

        $difference = $totalRecordCount - $recordCount;//54

        $pageCounter++; 
        $globalCount += $recordCount;


        array_push($promises,$url);

   } while ($totalRecordCount >= $globalCount); 
    return $promises;
}

$a = $runRequest();
print_r($a); //contains array of endpoint like in the sample above

【问题讨论】:

    标签: php guzzle6 guzzle


    【解决方案1】:

    存在无限循环,因为当总记录数等于全局计数时,您会继续循环。第 3 页及以上有 0 条记录,因此总数为 154。将 >= 替换为 > 将解决循环问题。

    但是,代码仍然无法按您预期的那样工作。对于每个页面,您使用getAsync() 准备一个请求,然后立即执行wait()then 语句什么也不做。它返回响应,默认情况下它已经这样做了。所以在实践中,这些都是同步请求。

    鉴于页面大小是恒定的,您可以根据第一次请求时给出的信息计算出您需要的页面。

    function runRequest(){
    
        $promises = [];
        $client = new \GuzzleHttp\Client();
        
        $url = 'https://api.com/opportunities_dates?key='.$GLOBALS['API_KEY'].'&fields=["fields"]';
    
        // Initial request to get total record count and page count
        $initialRequest = $client->getAsync($url.'&page=1');
        $initialResponse = $initialRequest->wait();
        $initialBody = json_decode($initialResponse->getBody());
    
        $promises[] = $initialRequest;
    
        $totalRecordCount = $initialBody->totalRecordCount;//154 
        $pageSize = $initialBody->pageSize;//100
        $nrOfPages = ceil($totalRecordCount / $pageSize);//2
    
        for ($page = 2; $page <= $nrOfPages; $page++) {
            $promises[] = $client->getAsync($url.'&page='.$page);
        }
    
        return $promises;
    }
    
    $promises = runRequest();
    $responses = \GuzzleHttp\Promise\unwrap($promises);
    

    请注意,该函数现在返回 Promise 而不是字符串形式的 URL。

    第一个承诺已经完成并不重要。 unwrap 函数不会导致对第 1 页的另一个 GET 请求,而是返回现有响应。对于所有其他页面,请求是同时完成的。

    【讨论】:

    • 嘿,谢谢@Arnold,顺便说一句,我在这行有错误。 $responses = \Guzzle\Promise\unwrap($promises);我需要在文件的最顶部声明​​一些东西吗?
    • 我的错,命名空间是GuzzleHttpdocs.guzzlephp.org/en/stable/…
    猜你喜欢
    • 2018-02-07
    • 2017-08-28
    • 2020-07-14
    • 2020-02-08
    • 2019-12-12
    • 1970-01-01
    • 2018-03-21
    • 1970-01-01
    • 2016-03-21
    相关资源
    最近更新 更多