简体   繁体   中英

how to auto loop for get all pages and merge pages in unique json?

I need get json multpages API responses and merge results in an unique json.

The API GET, returns 50 collections per page.

I need:

  1. Generate curl request loop;
  2. Break Loop when last page response empty;
  3. Merge Json responses from pages into one (unique) json response;

My working PHP Code:

 $p = 1;

 $curl = curl_init();

 curl_setopt_array($curl, array(
     CURLOPT_URL => "https://demourl.com/api-v1/products/?page=' . $p . '",
     CURLOPT_RETURNTRANSFER => true,
     CURLOPT_ENCODING => "",
     CURLOPT_MAXREDIRS => 10,
     CURLOPT_TIMEOUT => 30,
     CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
     CURLOPT_CUSTOMREQUEST => "GET",
     CURLOPT_POSTFIELDS => "",
     CURLOPT_COOKIE => "ckAPI=lkjk09je1iiw90q882388mfvufuiag6f5n4dodr4",
     CURLOPT_HTTPHEADER => array(
         "authorization: Basic Og==",
         "content-type: application/json",
         "x-api-key: 85f8287ae41o2o3j4h56u7ub3416a1f7d06b2c5",
         "x-app-key: m1n2b3v4v4qwrty0192of9mawo0LswostOjeXoW"
     ),
 ));

 $response = curl_exec($curl);

 $err = curl_error($curl);

 curl_close($curl);

 if ($err) {
     echo "cURL Error #:" . $err;
 } else {
     header('Content-type: Application/JSON');
     $j = json_decode($response);
     echo  json_encode($j, JSON_PRETTY_PRINT);
 }

My code work fine, but this code no generates loop.

Please help me. Tks.

I found one solution, but a find other options too.

The solution and working code, is this:

<?php

// abre curl
$curl = curl_init();
$result = array();
$numberOfPages = 5;
for($i = 1; $i < $numberOfPages; $i++) {
    $url = sprintf("https://myurl.com/api-v1/products/?page=%d", $i);


curl_setopt_array($curl, array(
  CURLOPT_URL => $url,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_POSTFIELDS => "",
  CURLOPT_COOKIE => "ckreg=9df47237df410nknvufuiag6f5n4dodr4",
  CURLOPT_HTTPHEADER => array(
    "authorization: Basic Og==",
    "content-type: application/json",
    "x-api-key: 1l2k3j3h4a0s9d8fg8g7gh7lkjasdfh",
    "x-app-key: 4z1x2c23v4b5n6n7mqwerpy0876lkbj"
  ),
));


$response = curl_exec($curl);

$err = curl_error($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {

 $j = json_decode($response, true);

 $result[] = $j;

}


}

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
 header('Content-type: Application/JSON');
$result_M = json_encode($result, JSON_PRETTY_PRINT);
print_r( $result_M ); 

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM