Hi,
I’m getting a list of products via an API which is split over multiple pages, so I have to loop over the pages until there are no more pages and save the data in a variable. So here is the basic curl request which is fine:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://myurl/products?page_no=1",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS =>"{\n \"email\": $user ,\n \"password\": $pw \n}",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer $tokenout",
"Content-Type: application/json"
),
));
$rsp = curl_exec($curl);
curl_close($curl);
So does anyone have any advice as to the best approach is. My idea was to wrap the request in a function and instead of hard coding the page number increment it inside a variable. It would be something like this:
$page = 1;
function multipleCurlRequests()
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://myurl/products? . $page . ",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS =>"{\n \"email\": $user ,\n \"password\": $pw \n}",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer $token",
"Content-Type: application/json"
),
));
if (curl_errno($curl)) {
echo 'Error:' . curl_error($curl);
return;
} else {
$page++;
multipleCurlRequests();
$rsp = curl_exec($curl);
curl_close($curl);
}
}
// Call first
multipleCurlRequests();
This seems like a really hashed approach though, I am not sure what will happen in terms of performance trying to retrieve the data over multiple pages like this.
There seems to be little information how specifically to handle this.
Thanks in advance.