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);
?>