How to run wp_remote_get() inside of a loop for multiple page API response?

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/productspage=' . $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
}

Topic wp-remote-get Wordpress

Category Web


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.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.