Get and write all paginated data in a file via looped PHP API cURL
<p>As a somewhat newbie to making API requests and coding appropriately, I would like help writing the appropriate code to loop, move the cursor to the next page and return all the data until there are no more pages. My initial code gets the first page of 50 results without looping. I just need help writing a proper loop. </p>
<p>The initial code is as follows. It works well to get the first page of results. I output the results to the screen just to test and see what the output is. There isn't any error handling in the code. </p>
<p><strong>I need to add a loop in my code until hasMore is false and then write/append the data to my file. </strong></p>
<p>The API documentation indicates that I can move the cursor and get the next page in the following way.
This endpoint performs pagination via the cursor. The pageInfo attribute will contain information about whether there are more results: {"cursor": "Mg", "hasMore": true}. If hasMore is true, the cursor can be passed into the next API request as part of the query string to get the next page of results, e.g. ?cursor=Mg. </p>
<pre class="brush:php;toolbar:false;"><?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);
?></pre></p>