1

I'm using the XeroOAuth-PHP SDK and looking to download Invoices for a private application - so far so good in authenticating and downloading the first batch of 100 invoices.

I'm now looking to extend the code to include pagination to download groups of 100 invoices at a time - I can get the count of invoices for each request using:

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

but not sure how to add a loop to start at page 1 and continue until the count of invoices is less than 100?

Here's the request that gets the first 100 Accounts Receivable invoices:

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

I'm looking for something along these lines:

// 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++;         
NetVicious
  • 3,848
  • 1
  • 33
  • 47
user982124
  • 4,416
  • 16
  • 65
  • 140

1 Answers1

0

Try with this:

// 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    
}
NetVicious
  • 3,848
  • 1
  • 33
  • 47