Get and write all paginated data in a file via looped PHP API cURL
P粉652523980
P粉652523980 2023-09-01 10:42:29
0
1
403
<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>
P粉652523980
P粉652523980

Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!