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