最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - cURL needing to loop through all "next_page"

programmeradmin1浏览0评论

When using cURL, how would I be able to include a call inside my get_all that basically will loop through all the next_pages, get the data and then outpul it to $response->data when the "next_page" parameter becomes null?

Here is the method:

public function get_all()
{
    return $response->data;
}

This is what $response->data is returning as of now (The cURL code wasn't included here):

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

When using cURL, how would I be able to include a call inside my get_all that basically will loop through all the next_pages, get the data and then outpul it to $response->data when the "next_page" parameter becomes null?

Here is the method:

public function get_all()
{
    return $response->data;
}

This is what $response->data is returning as of now (The cURL code wasn't included here):

"paginator": {
    "total": 3092,
    "per_page": 500,
    "current_page": 2,
    "last_page": 7,
    "prev_page": "https://oc/api/v1/employees/?page=1",
    "next_page": "https://oc/api/v1/employees/?page=3"
},
"data": [
        {
            "id": 1592,
        etc....
Share Improve this question edited Nov 4, 2019 at 6:44 DevSem asked Oct 30, 2019 at 12:57 DevSemDevSem 2092 silver badges11 bronze badges 5
  • Hello! I'm not seeing any WordPress code or site in this question, I'm not seeing any curl code/commands either. Are these requests being made to a 3rd party or is this the WP REST API? There's large chunks of missing code, it's not possible to fully understand or debug what you're doing with the code available, please edit your question to clarify and make things clearer, as well as how you're using curl, and what you're using it with – Tom J Nowell Commented Oct 30, 2019 at 13:56
  • @TomJNowell, I went ahead and included the cURL request – DevSem Commented Oct 30, 2019 at 14:17
  • Is this inside WP calling another site? Or is this non-WP code calling a WordPress site? – Tom J Nowell Commented Oct 30, 2019 at 14:19
  • @TomJNowell It's a non-WP site calling into WordPress - Everything is working perfectly, I'm just getting the max 500 results, so I just want to figure out a way to loop through the 'next_page's until it reached null and then output all the data into $response->data. – DevSem Commented Oct 30, 2019 at 14:24
  • Here is the full code for reference: jsfiddle/tLko7udm @TomJNowell – DevSem Commented Oct 30, 2019 at 14:27
Add a comment  | 

1 Answer 1

Reset to default 0

You could try something like:

$url = 'https://oc/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;
发布评论

评论列表(0)

  1. 暂无评论