I need get json multpages API responses and merge results in an unique json.
The API GET, returns 50 collections per page.
I need:
- Generate curl request loop;
- Break Loop when last page response empty;
- 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.