PHP API cURL with a loop that that gets all paginated data and writes it to a file

Viewed 27

As a somewhat newbie for making API requests and coding appropriately, I am hoping I can get help with the proper loop code for moving the cursor to the next page and returning all the data until there are no more pages. My initial code works for getting the first page of 50 results and has no loop statement. I just need help writing the proper loop.

Initial code is below. It works fine to get the first page of results. I dump to screen just as a test to see what is coming out. I don't have any error handling in the code.

I need to add the loop in the code that that gathers all the data until hasMore is false and writes/appends that to my file.

The API documentation indicates that I can move the cursor and get the next page via this. This endpoint is paginated via cursor. The pageInfo attribute will contain information on whether there are more results or not: { "cursor": "Mg", "hasMore": true }. If hasMore is true, you can retrieve the next page of results by passing the cursor into your next API request as part of the query string, e.g. ?cursor=Mg.

<?php

$url = "myURL/incoming/v2/content";

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$headers = array(
   "Accept: application/json",
   "Authorization: Bearer key",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
file_put_contents('CURL-CONTENT.txt', $resp);

?>
1 Answers

You are leaving out some details. I can give you some generalized help.
This may be enough to get you on track.

file_put_contents('CURL-CONTENT.txt',''); // create file and if it exists, clear its contents
$url = "myURL/incoming/v2/content/";
while(true){
  $curl = curl_init($url);
  curl_setopt($curl, CURLOPT_URL, $url);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  
  $headers = array(
     "Accept: application/json",
     "Authorization: Bearer key",
  );
  curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  //for debug only!
  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  
  $resp = curl_exec($curl);
  curl_close($curl);
  var_dump($resp);
  file_put_contents('CURL-CONTENT.txt', $resp,FILE_APPEND); // Appends to the file
 //  get the value of hasMore. 
  if(!hasMore){break;}
 // at this point there must be more, so prepare the next URL
  $url = "myURL/incoming/v2/content/?Cursor=Mg";
}
Related