public function parseResponse($rawResponseArray, $finalPattern = self::API_PARSE_PATTERN_NONE, $levelsDeep = 2) { if (is_null($finalPattern)) { $finalPattern = self::API_PARSE_PATTERN_NONE; } if (!array_key_exists('failedConnectionInfo', $rawResponseArray)) { if ($rawResponseArray['status'] > 200) { ob_start(); print_r($rawResponseArray['failedConnectionInfo']); $rawFail = ob_get_contents(); ob_end_clean(); $prev = Saf_Debug::isEnabled() ? new Exception(htmlentities($rawFail)) : NULL; throw new Saf_Exception_BadGateway('The scheduling system failed. ', $rawResponseArray['status'], $prev); } $xmlResult = simplexml_load_string($rawResponseArray['raw'], 'SimpleXMLElement', 0, 'http://www.w3.org/2003/05/soap-envelope', FALSE); if ($xmlResult) { $envelope = $xmlResult->children('http://www.w3.org/2003/05/soap-envelope'); $current = $envelope; for ($i = 0; $i < $levelsDeep; $i++) { $current = $current->children(); } $payloadXml = (string) $current; $data = simplexml_load_string($payloadXml); $parsedData = Saf_Config::arrayMap($data); if (is_array($parsedData) && array_key_exists('Error', $parsedData)) { if (is_array($parsedData['Error']) && array_key_exists('Message', $parsedData['Error'])) { $message = $parsedData['Error']['Message']; $userMessage = Saf_Debug::isEnabled() ? $message : 'Server returned an error message that has been logged'; //#TODO #1.1.0 decide how to handle error logging throw new Saf_Exception_Upstream($message, 0); } else { Saf_Debug::outData(array("XML Client Error Message " => $parsedData['Error'])); throw new Saf_Exception_Upstream('Server returned error with no message', 0); } } return $parsedData ? $finalPattern == self::API_PARSE_PATTERN_NONE ? $parsedData : current($parsedData) : NULL; } else { $head = str_replace("\r\n", "\\r\\n<br/>", $rawResponseArray['receivedHeaders']); $body = str_replace("\r\n", "\\r\\n<br/>", $rawResponseArray['raw']); $libXmlErrors = libxml_get_errors(); $xmlErrors = array(); $errorMap = array(LIBXML_ERR_WARNING => 'LIBXML_ERR_WARNING', LIBXML_ERR_ERROR => 'LIBXML_ERR_ERROR', LIBXML_ERR_FATAL => 'LIBXML_ERR_FATAL'); foreach ($libXmlErrors as $error) { $xmlErrors[] = "{$error->level} {$error->code}" . ($error->file ? " in {$error->file}" : "") . " on line {$error->line},{$error->column}" . ($error->message ? ": {$error->message}" : ''); } $libXmlErrors = 'LIB_XML_ERRORS: <br/>' . implode('<br/>', $xmlErrors) . '<br/>BAD_XML: ' . htmlentities($rawResponseArray['raw']) . '<br/>SERVER_HEADERS: ' . htmlentities($head) . '<br/>SERVER_BODY: ' . htmlentities($body); throw new Exception('Unable to parse response XML', 0, Saf_Debug::isEnabled() ? new Exception($libXmlErrors) : NULL); } } else { ob_start(); print_r($rawResponseArray['failedConnectionInfo']); $rawFail = ob_get_contents(); ob_end_clean(); if ($rawResponseArray['status'] == 0) { if ($rawResponseArray['failedConnectionInfo']['connect_time'] > $this->_client->getConnectionTimeout()) { throw new Saf_Exception_GatewayTimeout('Connection to the remote system timed out.'); } else { if ($rawResponseArray['failedConnectionInfo']['total_time'] > $this->_client->getTimeout()) { throw new Saf_Exception_GatewayTimeout('Response from the remote system timed out.'); } } $prev = new Exception(htmlentities($rawFail)); throw new Saf_Exception_BadGateway('Unable to contact the remote system.', $rawResponseArray['status'], $prev); } $rawRequest = array_key_exists('request', $rawResponseArray) ? 'RAW_REQUEST ' . (array_key_exists('request', $rawResponseArray) ? htmlentities($rawResponseArray['request']) : '') : ''; $prev = Saf_Debug::isEnabled() ? new Exception('RAW_FAIL ' . htmlentities($rawFail) . '<br/>' . ($rawRequest ? htmlentities($rawRequest) . '<br/>' : '') . ('RAW_RESPONSE ' . htmlentities(htmlentities($rawResponseArray['raw'])))) : NULL; throw new Saf_Exception_BadGateway('Communication with the remote system failed.', $rawResponseArray['status'], $prev); } }