2017-04-04 13 views
0

私はXeroOAuth-PHP SDKを使用しており、プライベートアプリケーション用の請求書をダウンロードしようとしています - これまでの100個の請求書の最初のバッチを認証してダウンロードするのが得意です。XeroOAuth-PHP - ページネーションの例

私は今、一度に100枚の請求書のグループをダウンロードするには、ページ区切りが含まれるようにコードを拡張するために探しています - 私が使用して要求ごとに請求書の数を取得することができます:

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

ではなく、必ずどのようにページ1から開始するループを追加し、請求書の数が100未満になるまで続行しますか?

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

私はこれらの線に沿って何かを探しています:ここで

は、最初の100枚の売掛金の請求書を取得するリクエストです

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

答えて

0

これを試してみてください:

// 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  
} 
関連する問題