【问题标题】:XeroOAuth-PHP - Pagination ExampleXeroOAuth-PHP - 分页示例
【发布时间】:2017-04-04 04:42:01
【问题描述】:

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

【问题讨论】:

    标签: php loops xero-api


    【解决方案1】:

    试试这个:

    // 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    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-17
      • 2016-04-27
      • 1970-01-01
      • 2012-10-12
      • 2018-03-01
      • 2014-01-08
      • 2013-09-06
      • 2015-03-05
      相关资源
      最近更新 更多