2014-05-18 83 views
2

我使用PHP连接MySQL数据库和第三方API。当使用下面的脚本时,我不断收到超时错误。我使用的API的所有者建议将每次调用限制为50条记录。我是PHP新手,尽管我的所有Google搜索都无法解决如何进行批处理。脚本如下:如何分批处理PHP?

<?php 

include('config.inc.php'); 


$conn = new mysqli($hostname, $username, $passwd, $db); 

if ($conn->connect_error) { 
    echo 'Database connection failed...' . 'Error: ' . $conn->connect_errno . ' ' . $conn->connect_error; 
    exit; 
} else { 
    $conn->set_charset('utf8'); 
} 

$sql = "SELECT Duedate, Invoicenumber, customername, txndate, itemref_fullname, xeroaccountnumber, Description, Quantity, rate, XEROTAXTYPE FROM invoicelinedetail"; 

$rs = $conn->query($sql); 

if ($rs == false) { 

} else { 

    require('xeroconfig.php'); 

    $XeroOAuth = new XeroOAuth(array_merge(array(
    'application_type' => XRO_APP_TYPE, 
    'oauth_callback' => OAUTH_CALLBACK, 
    'user_agent' => $useragent 
), $signatures)); 


    $initialCheck = $XeroOAuth->diagnostics(); 
    $checkErrors = count($initialCheck); 
    if ($checkErrors > 0) { 
    // you could handle any config errors here, or keep on truckin if you like to live dangerously 
    foreach ($initialCheck as $check) { 
     echo 'Error: ' . $check . PHP_EOL; 
    } 
    } else { 
    $session = persistSession(array(
     'oauth_token' => $XeroOAuth->config ['consumer_key'], 
     'oauth_token_secret' => $XeroOAuth->config ['shared_secret'], 
     'oauth_session_handle' => '' 
    )); 
    $oauthSession = retrieveSession(); 

    if (isset($oauthSession ['oauth_token'])) { 
     $XeroOAuth->config ['access_token'] = $oauthSession ['oauth_token']; 
     $XeroOAuth->config ['access_token_secret'] = $oauthSession ['oauth_token_secret']; 

     $xml = "<Invoices>\n"; 

     foreach ($rs as $row) { 
     $xml .= "<Invoice>\n"; 
     $xml .= "<Type>ACCREC</Type>\n"; 
     $xml .= "<Contact>\n"; 
     $xml .= "<Name>" . xmlEscape($row['customername']) . "</Name>\n"; 
     $xml .= "</Contact>\n"; 
     $xml .= "<Date>" . xmlEscape($row['txndate']) . "</Date>\n"; 
     $xml .= "<DueDate>" . xmlEscape($row['Duedate']) . "</DueDate>\n"; 
     $xml .= "</Invoice>\n"; 
     } 

     $xml .= "</Invoices>"; 
     #echo $xml; 

     $response = $XeroOAuth->request('POST', $XeroOAuth->url('Invoices', 'core'), array(), $xml); 

     if ($XeroOAuth->response['code'] == 200) { 
     $invoice = $XeroOAuth->parseResponse($XeroOAuth->response['response'], $XeroOAuth->response['format']); 
     echo "" . count($invoice->invoices[0]) . " invoice created/updated in this Xero organisation."; 
     if (count($invoice->Invoices[0]) > 0) { 
      echo "The first one is: </br>"; 
      pr($Invoice->Invoices[0]->Invoice); 
     } 
     } else { 
     outputError($XeroOAuth); 
     } 
    } 
    } 
} 

回答

0

您的情况使用%运算符为每50张发票提出请求。像这样的东西(你可能不得不改变计数($ rs)到其他东西取决于它是什么类型的对象)。

<?php 

include('config.inc.php');  

$conn = new mysqli($hostname, $username, $passwd, $db); 

if ($conn->connect_error) { 
    echo 'Database connection failed...' . 'Error: ' . $conn->connect_errno . ' ' . $conn->connect_error; 
    exit; 
} else { 
    $conn->set_charset('utf8'); 
} 

$sql = "SELECT Duedate, Invoicenumber, customername, txndate, itemref_fullname, xeroaccountnumber, Description, Quantity, rate, XEROTAXTYPE FROM invoicelinedetail"; 

$rs = $conn->query($sql); 

if ($rs == false) { 

} else { 

    require('xeroconfig.php'); 

    $XeroOAuth = new XeroOAuth(array_merge(array(
    'application_type' => XRO_APP_TYPE, 
    'oauth_callback' => OAUTH_CALLBACK, 
    'user_agent' => $useragent 
), $signatures)); 


    $initialCheck = $XeroOAuth->diagnostics(); 
    $checkErrors = count($initialCheck); 
    if ($checkErrors > 0) { 
    // you could handle any config errors here, or keep on truckin if you like to live dangerously 
    foreach ($initialCheck as $check) { 
     echo 'Error: ' . $check . PHP_EOL; 
    } 
    } else { 
    $session = persistSession(array(
     'oauth_token' => $XeroOAuth->config ['consumer_key'], 
     'oauth_token_secret' => $XeroOAuth->config ['shared_secret'], 
     'oauth_session_handle' => '' 
    )); 
    $oauthSession = retrieveSession(); 

    if (isset($oauthSession ['oauth_token'])) { 
     $XeroOAuth->config ['access_token'] = $oauthSession ['oauth_token']; 
     $XeroOAuth->config ['access_token_secret'] = $oauthSession ['oauth_token_secret']; 
     $invoice_counter = 0; 

     foreach ($rs as $row) { 
     if(++$invoice_counter % 50 === 1) { 
      $xml = "<Invoices>\n"; 
     } 

     $xml .= "<Invoice>\n"; 
     $xml .= "<Type>ACCREC</Type>\n"; 
     $xml .= "<Contact>\n"; 
     $xml .= "<Name>" . xmlEscape($row['customername']) . "</Name>\n"; 
     $xml .= "</Contact>\n"; 
     $xml .= "<Date>" . xmlEscape($row['txndate']) . "</Date>\n"; 
     $xml .= "<DueDate>" . xmlEscape($row['Duedate']) . "</DueDate>\n"; 
     $xml .= "</Invoice>\n"; 

     if($invoice_counter % 50 === 0 || $invoice_counter == count($rs)) { 
      $xml .= "</Invoices>\n"; 
      #echo $xml; 

      $response = $XeroOAuth->request('POST', $XeroOAuth->url('Invoices', 'core'), array(), $xml); 

      if ($XeroOAuth->response['code'] == 200) { 
      $invoice = $XeroOAuth->parseResponse($XeroOAuth->response['response'], $XeroOAuth->response['format']); 
      echo "" . count($invoice->invoices[0]) . " invoice created/updated in this Xero organisation."; 
      if (count($invoice->Invoices[0]) > 0) { 
       echo "The first one is: </br>"; 
       pr($Invoice->Invoices[0]->Invoice); 
      } 
      } else { 
      outputError($XeroOAuth); 
      }   
     } 
     } 
    } 
    } 
}