// Get the headers. $uniqueHeaders = ZuoraAPIHelper::getCSVHeaders($xml_obj); ZuoraAPIHelper::getCSVData($xml_obj, $uniqueHeaders, true, true); } else { $responseString = xml_pretty_printer($xml, true); } $queryLocator = ZuoraAPIHelper::getQueryLocator($xml); while ($outputQM && $queryLocator) { $payload = "<ns1:queryMore><ns1:queryLocator>" . $queryLocator . "</ns1:queryLocator></ns1:queryMore>"; $soapRequest = ZuoraAPIHelper::createRequestAndHeadersWithNS($header->data["session"], $_SESSION['api-batchSize'], $callOptions, $payload, $_SESSION['api-ns'], $_SESSION['object-ns']); $timeBefore = microtime(true); $xml = ZuoraAPIHelper::callAPIWithClient($client, $header, $soapRequest, $debug); $timings[] = microtime(true) - $timeBefore; $queryLocator = ZuoraAPIHelper::getQueryLocator($xml); if ($outputCSV) { $xml_obj = ZuoraAPIHelper::getElementFromXML($xml); ZuoraAPIHelper::getCSVData($xml_obj, $uniqueHeaders, true, false); } else { $responseString .= "\n" . xml_pretty_printer($xml, true); $requestString .= "\n" . xml_pretty_printer($soapRequest, true); } } if ($outputCSV) { exit; } } catch (Exception $e) {
public static function bulkOperation($client, $header, $method, $payload, $itemCount, $debug, $htmlOutput = FALSE, $zObjectCap = -1) { global $maxZObjectCount; global $defaultApiNamespaceURL; $result = array("errorList" => array(), "batchList" => array(), "successCount" => 0, "errorCount" => 0); // Only create/update calls are supported. if ($method != "create" && $method != "update" && $method != "delete") { return $result; } $nodeName = "zObjects"; if ($method == "delete") { $nodeName = "ids"; } // Allow control over how many objects are submitted in one call. if ($zObjectCap < 0) { $zObjectCap = $maxZObjectCount; } $soapRequest = createRequest($header->data["session"], $payload); $xml_obj = new SimpleXMLElement($soapRequest); $xml_obj->registerXPathNamespace("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/"); $xml_obj->registerXPathNamespace("ns1", $defaultApiNamespaceURL); $type = $xml_obj->xpath("//SOAP-ENV:Envelope/SOAP-ENV:Body/ns1:" . $method . "/ns1:type"); // Iterate through the list of items, $zObjectCap at a time for ($counter = 0; $counter < $itemCount; $counter += $zObjectCap) { // Identify upper bound for this batch. $lowerBound = $counter + 1; $upperBound = $counter + $zObjectCap; if ($upperBound > $itemCount) { $upperBound = $itemCount; } // Create request for this batch of records. $batchNodes = $xml_obj->xpath("//SOAP-ENV:Envelope/SOAP-ENV:Body/ns1:" . $method . "/ns1:" . $nodeName . "[position()>=" . $lowerBound . " and position()<" . ($upperBound + 1) . "]"); $batchPayload = "\n<ns1:" . $method . ">\n"; if ($method == "delete") { $batchPayload = $batchPayload . " " . $type[0]->asXML() . "\n"; } for ($i = 0; $i < count($batchNodes); $i++) { $batchPayload = $batchPayload . " " . $batchNodes[$i]->asXML() . "\n"; } $batchPayload = $batchPayload . "</ns1:" . $method . ">\n"; print "Batch " . ceil($upperBound / $zObjectCap) . ": submitting ZObjects " . $lowerBound . "-" . $upperBound . " (" . count($batchNodes) . ")..."; $soapRequest = createRequest($header->data["session"], $batchPayload); // Execute the API call. $timeBefore = microtime(true); $batchResponse = ZuoraAPIHelper::callAPIWithClient($client, $header, $soapRequest, $debug); $timeAfter = microtime(true); print " " . ($timeAfter - $timeBefore) . " secs - Done."; if ($htmlOutput) { print "<br/>"; } print "\n"; // Parse through the response. $xml_obj2 = new SimpleXMLElement($batchResponse); $xml_obj2->registerXPathNamespace("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/"); $xml_obj2->registerXPathNamespace("ns1", $defaultApiNamespaceURL); $soapFaultNode = $xml_obj2->xpath("//SOAP-ENV:Envelope/SOAP-ENV:Body/SOAP-ENV:Fault"); $resultNodes = $xml_obj2->xpath("//SOAP-ENV:Envelope/SOAP-ENV:Body/ns1:" . $method . "Response/ns1:result/ns1:Success | //SOAP-ENV:Envelope/SOAP-ENV:Body/ns1:" . $method . "Response/ns1:result/ns1:success"); $successCount = 0; $errorCount = 0; if (count($soapFaultNode) > 0) { $errorCount = count($batchNodes); for ($i = 0; $i < count($batchNodes); $i++) { $faultCode = $xml_obj2->xpath("//SOAP-ENV:Envelope/SOAP-ENV:Body/SOAP-ENV:Fault/faultstring"); $faultMessage = $xml_obj2->xpath("//SOAP-ENV:Envelope/SOAP-ENV:Body/SOAP-ENV:Fault/detail/Exception"); array_push($result["errorList"], array("index" => $lowerBound + $i, "code" => (string) $faultCode[0], "message" => (string) $faultMessage[0])); } array_push($result["batchList"], array("start" => $lowerBound, "end" => $upperBound, "size" => count($batchNodes), "successCount" => $successCount, "errorCount" => $errorCount)); } else { for ($i = 0; $i < count($resultNodes); $i++) { $resultNode = $resultNodes[$i]; if (strcasecmp($resultNode, "false") == 0) { $errorCodeNodes = $xml_obj2->xpath("//SOAP-ENV:Envelope/SOAP-ENV:Body/ns1:" . $method . "Response/ns1:result[position()=" . ($i + 1) . "]/ns1:errors/ns1:Code | //SOAP-ENV:Envelope/SOAP-ENV:Body/ns1:" . $method . "Response/ns1:result[position()=" . ($i + 1) . "]/ns1:Errors/ns1:Code"); $errorMsgNodes = $xml_obj2->xpath("//SOAP-ENV:Envelope/SOAP-ENV:Body/ns1:" . $method . "Response/ns1:result[position()=" . ($i + 1) . "]/ns1:errors/ns1:Message | //SOAP-ENV:Envelope/SOAP-ENV:Body/ns1:" . $method . "Response/ns1:result[position()=" . ($i + 1) . "]/ns1:Errors/ns1:Message"); $errorCount++; array_push($result["errorList"], array("index" => $lowerBound + $i, "code" => (string) $errorCodeNodes[0], "message" => (string) $errorMsgNodes[0])); } else { $successCount++; } } array_push($result["batchList"], array("start" => $lowerBound, "end" => $upperBound, "size" => count($resultNodes), "successCount" => $successCount, "errorCount" => $errorCount)); } $result["successCount"] += $successCount; $result["errorCount"] += $errorCount; } return $result; }