XeroOAuth-PHP - 分页示例

时间:2017-04-04 04:42:01

标签: php loops xero-api

我正在使用XeroOAuth-PHP SDK并希望下载私人应用程序的发票 - 到目前为止,在验证和下载第一批100张发票时非常好。

我现在正在寻求扩展代码以包括分页以一次下载100个发票组 - 我可以使用以下方式获取每个请求的发票数:

$totalInvoices = count($invoices->Invoices[0]);

但不确定如何添加循环以从第1页开始并继续直到发票数量少于100?

以下是获取前100个应收帐款发票的请求:

$response = $XeroOAuth->request('GET', $XeroOAuth->url('Invoices', 'core'), array('where' => 'Type=="ACCREC"'));

我正在寻找这些方面的东西:

// set pagiation to page 1
$page = 1;

// start a loop for the $page counter

// download first page of invoices (first 100) - not sure how to specify page 1 here
$response = $XeroOAuth->request('GET', $XeroOAuth->url('Invoices', 'core'), array('where' => 'Type=="ACCREC"', 'page' => $page ));

    if ($XeroOAuth->response['code'] == 200) {

        // Get total found invoices
        $totalInvoices = count($invoices->Invoices[0]);

        // Parse Invoices               
        $invoices = $XeroOAuth->parseResponse($XeroOAuth->response['response'], $XeroOAuth->response['format']);

        // Loop through each invoice                
        $recnum = 1;

            foreach($invoices as $invoice){

            // Do Stuff 
            pr($invoices->Invoices[$recnum]->Invoice);

            $recnum++; 

            }

    } else {
        outputError($XeroOAuth);
    }   


// Exit once $totalInvoices < 100    

$page++;         

1 个答案:

答案 0 :(得分:0)

试试这个:

// set pagiation to page 1
$page = 1;
$stop_report = false;

// start a loop for the $page counter

while ( !$stop_report ) {

    // download first page of invoices (first 100) - not sure how to specify page 1 here
    $response = $XeroOAuth->request('GET', $XeroOAuth->url('Invoices', 'core'), array('where' => 'Type=="ACCREC"', 'page' => $page ));

    if ($XeroOAuth->response['code'] == 200) {

        // Get total found invoices
        $totalInvoices = count($invoices->Invoices[0]);

        // If we get less than 100 invoices that says this it's the last group of invoices 
        if ( $totalInvoices < 100 ) $stop_report = true;

        // Parse Invoices               
        $invoices = $XeroOAuth->parseResponse($XeroOAuth->response['response'], $XeroOAuth->response['format']);

        // Loop through each invoice                
        $recnum = 1;

        foreach($invoices as $invoice){

            // Do Stuff 
            pr($invoices->Invoices[$recnum]->Invoice);

            $recnum++; 

        }

    } else {
        $stop_report = true; // We got one error, so stop the loop 
        outputError($XeroOAuth);
    }   

    $page++;  // On the next call we should get the next page 


    // Exit once $totalInvoices < 100    
}