如何批量处理PHP?

时间:2014-05-18 08:01:04

标签: php xml

我使用PHP连接MySQL数据库和第三方API。使用以下脚本时,我不断收到超时错误。 API I的所有者建议将每次调用限制为50条记录。我是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);
      }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

您使用%运算符每50张发票发出一次请求。像这样的东西(你可能需要根据它的对象类型将count($ 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);
          }          
        }
      }
    }
  }
}