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
546
<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

reply all(1)
P粉275883973

You're missing some details. I can give you some general help.
This might be enough to get you on the right track.

file_put_contents('CURL-CONTENT.txt',''); // 创建文件并清空其内容(如果文件存在)
$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);
  // 仅用于调试!
  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); // 追加到文件中
 // 获取hasMore的值。
  if(!hasMore){break;}
 // 此时必须还有更多内容,因此准备下一个URL
  $url = "myURL/incoming/v2/content/?Cursor=Mg";
}
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!