0

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

2 Answers 2

1

This should work:

<?php

$breakFlag = 0;

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

do {

    // 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]);

        if ($totalInvoices < 100) {
            $breakFlag = 1;
        }

        // Loop through found invoices 
        if ($totalInvoices > 0) {

            foreach ($invoices->Invoices->Invoice as $invoice) {
                $invoiceNumber = $invoice->InvoiceNumber;
                // Process Invoice as required
            } 

        }

        $page += 1;

    } else {

        // Request Error
    } 
} while($breakFlag == 0)
Sign up to request clarification or add additional context in comments.

Comments

1

Personally, I'd wrap the code you have in a function and then invoke it from within itself for as long as you need to.

fetchInvoices(1);

function fetchInvoices($page) {
    // 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
            } 
        }

        if($totalInvoices == 100) {
            fetchInvoices($page + 1)
        }
    } else {

        // Request Error
    }
}   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.