Exemplo n.º 1
0
 /**
  * Internal method for posting the invocation to the Intacct XML Gateway
  *
  * @param String      $xml        the XML request document
  * @param api_session $session    an api_session instance with an active connection
  * @param string      $dtdVersion Either "2.1" or "3.0".  Defaults to "3.0"
  * @param boolean     $multiFunc  whether or not this invocation calls multiple methods.  Default is false
  *
  * @throws Exception
  * @return String the XML response document
  */
 private static function post($xml, api_session $session, $dtdVersion = "3.0", $multiFunc = false)
 {
     $sessionId = $session->sessionId;
     $endPoint = $session->endPoint;
     $senderId = $session->senderId;
     $senderPassword = $session->senderPassword;
     $transaction = $session->transaction ? 'true' : 'false';
     $templateHead = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<request>\n    <control>\n        <senderid>{$senderId}</senderid>\n        <password>{$senderPassword}</password>\n        <controlid>foobar</controlid>\n        <uniqueid>false</uniqueid>\n        <dtdversion>{$dtdVersion}</dtdversion>\n        {%validate}\n        <includewhitespace>false</includewhitespace>\n    </control>\n    <operation transaction='{$transaction}'>\n        <authentication>\n            <sessionid>{$sessionId}</sessionid>\n        </authentication>";
     $contentHead = "<content>\n            <function controlid=\"foobar\">";
     $contentFoot = "</function>\n        </content>";
     $templateFoot = "</operation>\n</request>";
     if (is_null($session->getResponseValidation())) {
         $templateHead = str_replace("{%validate}", '', $templateHead);
     } else {
         $templateHead = str_replace("{%validate}", '<validate>' . $session->getResponseValidation() . '</validate>', $templateHead);
     }
     if ($multiFunc) {
         $xml = $templateHead . $xml . $templateFoot;
     } else {
         $xml = $templateHead . $contentHead . $xml . $contentFoot . $templateFoot;
     }
     if (self::$dryRun == true) {
         self::$lastRequest = $xml;
         return null;
     }
     $count = 0;
     // retry five times on too many operations
     $res = "";
     while (true) {
         try {
             $res = api_post::execute($xml, $endPoint);
             api_post::validateResponse($res);
             break;
         } catch (Exception $ex) {
             if (strpos($ex->getMessage(), "too many operations") !== false || strpos($ex->getMessage(), "HTTP Response Code not 200") !== false) {
                 $count++;
                 if ($count >= 5) {
                     throw new Exception($ex);
                 }
             } else {
                 throw new Exception($ex);
             }
         }
     }
     return $res;
 }