CURL需要循环遍历所有“Next_Page”

时间:2019-10-30 作者:DevSem

当使用cURL时,我如何能够在get\\u all中包含一个调用,该调用基本上会在接下来的所有页面中循环,获取数据,然后在“next\\u page”参数变为null时将其输出到$response->data?

Here is the method:

public function get_all()
{
    return $response->data;
}
This is what $response->data is returning as of now (此处未包含cURL代码):

"paginator": {
    "total": 3092,
    "per_page": 500,
    "current_page": 2,
    "last_page": 7,
    "prev_page": "https://oc.com/api/v1/employees/?page=1",
    "next_page": "https://oc.com/api/v1/employees/?page=3"
},
"data": [
        {
            "id": 1592,
        etc....

1 个回复
最合适的回答,由SO网友:majick 整理而成

您可以尝试以下方式:

$url = \'https://oc.com/api/v1/employees/?page=1\';
$data = array();
while ( $url ) {
    $response = $this->request( $url );
    $data = array_merge( $data, $response->data );
    // if this is not the last page, get the next page URL
    if ( $response->paginator->current_page != $response->paginator->last_page ) {
        $url = $response->paginator->next_page;
    } else {$url = false;}
}
return $data;