1

我正在使用 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++;         
4

1 回答 1

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    
}
于 2017-04-04T07:12:39.703 回答