2017-04-04 5 views
0

Ich verwende das XeroOAuth-PHP SDK und suche nach Rechnungen für eine private Anwendung herunterladen - so weit so gut in der Authentifizierung und dem Herunterladen der ersten Charge von 100 Rechnungen.XeroOAuth-PHP - Paginierung Beispiel

ich jetzt bin auf der Suche um den Code zu erweitern Paginierung sind Gruppen von 100 Rechnungen auf einmal herunterladen - ich die Anzahl der Rechnungen für jede Anfrage erhalten kann mit:

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

aber nicht sicher, wie man Fügen Sie eine Schleife hinzu, um auf Seite 1 zu beginnen, und fahren Sie fort, bis die Anzahl der Rechnungen kleiner als 100 ist.

Hier ist die Forderung, dass die ersten 100 Debitorenrechnungen bekommt:

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

Ich suche in dieser Richtung etwas:

// 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++;   

Antwort

0

mit Try 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  
}