0

I am using wp_remote_get() to grab some JSON from an API call. Everything is working great except that the API I am using has a limit of 100 results per response and there are just over 300 results that I need to loop over. You can get additional pages by adding &page=2, &page=3, etc. to the API URL. How would I go about refactoring this code in order to check each page of results and append the results of each page to an array?

$page = '1';
$url = 'https://example.com/products&page=' . $page;
$results = array();
$request = wp_remote_get($url, $args);
if (!is_wp_error($request)) {
    $data = wp_remote_retrieve_body($request);
    $body = json_decode($data);

    foreach ($data as $datapoint) {
        array_push($results, $datapoint);
    }

    ++ $page;
    // Run additional wp_remote_get on next page
}

1 Answer 1

3

If I'm reading the docs right, wp_remote_get() will return a WP_Error on failure, but not necessarily on a 404/400/403 (etc) status code from your remote server. (Though that will depend in part on what your remote server sends back if there's an invalid request.)

You can check the status code using wp_remote_retrieve_response_code().

I'd use something like this:

$page = 1;
$results = array();
$url = 'https://example.com/products&page=' . $page;
$keep_going = true;
while ( $keep_going ) {
    $request = wp_remote_get( $url, $args ); // This assumes you've set $args previously
    if ( is_wp_error( $request ) ) {
        // Error out.
        $keep_going = false;
    }
    if ( $keep_going ) {
        $status = wp_remote_retrieve_response_code();
        if ( 200 != $status ) {
            // Not a valid response.
            $keep_going = false;
        }
    }
    if ( $keep_going ) {
        $data = wp_remote_retrieve_body($request);
        $body = json_decode($data);

        foreach ($data as $datapoint) {
            array_push($results, $datapoint);
        }

        ++ $page;
        // URL for the next pass through the while() loop
        $url = 'https://example.com/products&page=' . $page;
    }
}

This code is untested and probably inelegant, but it should provide a decent starting point for you. If you're unclear on while() loops, check the PHP docs.

1
  • 1
    This is a perfect starting point, thank you so much!
    – user13286
    Mar 29, 2022 at 18:58

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.