I am fetching order details through Flipkar order Api.It is gives first 20(0-20) result at a time and for next records it gives next page url. For fetching next 20 records(20-40) again we have to call curl with next page url and fetch orders .For this i am using code below:
$listingbulk=array();
$headers = array(
'Cache-Control: no-cache',
'Content-type: application/json',
'Authorization: Bearer '.$fkt
);
$bulkjson= '{
"filter": {
"orderDate": {
"fromDate": "'.$orderfrom.'",
"toDate": "'.$orderto.'"
}
}
}';
$urlbulk = "https://api.flipkart.net/sellers/v2/orders/search";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$urlbulk);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $bulkjson);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$resultbulksku = curl_exec($curl);
$listingbulk[] = json_decode($resultbulksku);
if (curl_errno($curl)) {
echo 'Error:' . curl_error($curl);
}
curl_close ($curl);
$nextPageUrl= $listingbulk[0]->nextPageUrl;
if ($nextPageUrl !=''){
$newpageurl= orderFk($nextPageUrl,$headers);
if ($newpageurl !='') {
$newpageurl2= orderFk($newpageurl,$headers);
if ($newpageurl2 !=''){
$newpageurl3= orderFk($newpageurl2,$headers);
}
}
}
Function is here :
function orderFk($nextPageUrl,$headers){
$fp = fopen('order/order'.$currenttime.'.csv',"a");
$urlbulk1 = "https://api.flipkart.net/sellers/v2/".$nextPageUrl;
$curl1 = curl_init();
curl_setopt($curl1, CURLOPT_URL,$urlbulk1);
curl_setopt($curl1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl1, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl1, CURLOPT_HTTPHEADER, $headers);
$resultbulksku1 = curl_exec($curl1);
$listingbulk1[] = json_decode($resultbulksku1);
if (curl_errno($curl1)) {
echo 'Error:' . curl_error($curl1);
}
curl_close ($curl1);
$listingbulk=$listingbulk1;
$newnextPageUrl= $listingbulk1[0]->nextPageUrl;
return $listingbulk;
}
I want to crate if else condition dynamically so if "next pageurl" is exist in response it should call again the same function with new url ( without calling function multiple times in if else condition). If anyone has solution for this please reply.(answer with working example would be more helpful)