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": "https://oc.com/api/v1/employees/?page=1",
    "next_page": "https://oc.com/api/v1/employees/?page=3"
},
"data": [
        {
            "id": 1592,
        etc....
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.net/tLko7udm @TomJNowell
    – DevSem
    Commented Oct 30, 2019 at 14:27

1 Answer 1

0

You could try something like:

$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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.