I'm working on a script that requests Invoices from an online service - 100 invoices are returned at a time. I've got the basic request working to return the first 100 invoices and loop through the results. Here's my current code:
// set pagination to page 1
$page = 1;
// Request Invoices
$response = $XeroOAuth->request('GET', $XeroOAuth->url('Invoices', 'core'), array( 'page' => $page ));
if ($XeroOAuth->response['code'] == 200) {
// Parse Invoices
$invoices = $XeroOAuth->parseResponse($XeroOAuth->response['response'], $XeroOAuth->response['format']);
// Get total found invoices
$totalInvoices = count($invoices->Invoices[0]);
// Loop through found invoices
if ($totalInvoices > 0) {
foreach ($invoices->Invoices->Invoice as $invoice) {
$invoiceNumber = $invoice->InvoiceNumber;
// Process Invoice as required
}
}
} else {
// Request Error
}
I need to extend this as follows:
- if 100 invoices were returned I need to increment $page by 1 and then perform another request to get the 2nd page of invoices and process those
- continue this until the total number of invoices returned for the last request is less than 100 then exit after that
I can't get the logic correct here to extend this with another loop to continue the requests until it gets less than 100 invoices