我通过Flipkar订单Api获取订单详细信息。它一次给出前20(0-20)个结果,对于下一个记录,它给出下一页url。 为了再次获取接下来的20条记录(20-40),我们必须使用下一页网址和获取订单调用curl。为此我使用下面的代码:
$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 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;
}
如果" next pageurl"在响应中存在它应该再次使用新url调用相同的函数(在if else条件下不多次调用函数)。 如果有人有解决方案,请回复。(回答工作示例会更有帮助)
答案 0 :(得分:0)
我建议像这样,内部顺序函数使用while循环,退出错误或循环超过maxpage。在$ list中,您有所有回复。
function orderFk($nextPageUrl,$headers){
$page = 1;
$maxpage = 100;
$exit = 0;
$list = array();
while(strlen($nextPageUrl)){
$urlbulk = "https://api.flipkart.net/sellers/v2/".$nextPageUrl;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$urlbulk);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$resultbulk = curl_exec($curl);
$nextPageUrl = '';
if(curl_errno($curl)) {
$exit = 1;
}
curl_close($curl); // close before exit while loop
if($exit==1 || $page>$maxpage){
break;
}
$listingbulk = json_decode($resultbulk);
if(strlen($listingbulk[0]->nextPageUrl)>0){
$nextPageUrl = $listingbulk[0]->nextPageUrl;
}
$list[] = $listingbulk;
$page++;
}
return $list;
}