public function parseResponse($fp)
{
$data = '';
while ($datum = fread($fp, 4096)) {
$data .= $datum;
}
// Display HTTP content for debugging
if ($this->debug === TRUE) {
echo "<pre>---DATA---\n" . htmlspecialchars($data) . "\n---END DATA---\n\n</pre>";
}
// Check for data
if ($data === '') {
error_log($this->xmlrpcstr['no_data']);
return new XML_RPC_Response(0, $this->xmlrpcerr['no_data'], $this->xmlrpcstr['no_data']);
}
// Check for HTTP 200 Response
if (strpos($data, 'HTTP') === 0 && !preg_match('/^HTTP\\/[0-9\\.]+ 200 /', $data)) {
$errstr = substr($data, 0, strpos($data, "\n") - 1);
return new XML_RPC_Response(0, $this->xmlrpcerr['http_error'], $this->xmlrpcstr['http_error'] . ' (' . $errstr . ')');
}
//-------------------------------------
// Create and Set Up XML Parser
//-------------------------------------
$parser = xml_parser_create($this->xmlrpc_defencoding);
$pname = (string) $parser;
$this->xh[$pname] = array('isf' => 0, 'ac' => '', 'headers' => array(), 'stack' => array(), 'valuestack' => array(), 'isf_reason' => 0);
xml_set_object($parser, $this);
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, TRUE);
xml_set_element_handler($parser, 'open_tag', 'closing_tag');
xml_set_character_data_handler($parser, 'character_data');
//xml_set_default_handler($parser, 'default_handler');
// Get headers
$lines = explode("\r\n", $data);
while ($line = array_shift($lines)) {
if (strlen($line) < 1) {
break;
}
$this->xh[$pname]['headers'][] = $line;
}
$data = implode("\r\n", $lines);
// Parse XML data
if (!xml_parse($parser, $data, count($data))) {
$errstr = sprintf('XML error: %s at line %d', xml_error_string(xml_get_error_code($parser)), xml_get_current_line_number($parser));
$r = new XML_RPC_Response(0, $this->xmlrpcerr['invalid_return'], $this->xmlrpcstr['invalid_return']);
xml_parser_free($parser);
return $r;
}
xml_parser_free($parser);
// Got ourselves some badness, it seems
if ($this->xh[$pname]['isf'] > 1) {
if ($this->debug === TRUE) {
echo "---Invalid Return---\n" . $this->xh[$pname]['isf_reason'] . "---Invalid Return---\n\n";
}
return new XML_RPC_Response(0, $this->xmlrpcerr['invalid_return'], $this->xmlrpcstr['invalid_return'] . ' ' . $this->xh[$pname]['isf_reason']);
} elseif (!is_object($this->xh[$pname]['value'])) {
return new XML_RPC_Response(0, $this->xmlrpcerr['invalid_return'], $this->xmlrpcstr['invalid_return'] . ' ' . $this->xh[$pname]['isf_reason']);
}
// Display XML content for debugging
if ($this->debug === TRUE) {
echo '<pre>';
if (count($this->xh[$pname]['headers'] > 0)) {
echo "---HEADERS---\n";
foreach ($this->xh[$pname]['headers'] as $header) {
echo $header . "\n";
}
echo "---END HEADERS---\n\n";
}
echo "---DATA---\n" . htmlspecialchars($data) . "\n---END DATA---\n\n---PARSED---\n";
var_dump($this->xh[$pname]['value']);
echo "\n---END PARSED---</pre>";
}
// Send response
$v = $this->xh[$pname]['value'];
if ($this->xh[$pname]['isf']) {
$errno_v = $v->me['struct']['faultCode'];
$errstr_v = $v->me['struct']['faultString'];
$errno = $errno_v->scalarval();
if ($errno === 0) {
// FAULT returned, errno needs to reflect that
$errno = -1;
}
$r = new XML_RPC_Response($v, $errno, $errstr_v->scalarval());
} else {
$r = new XML_RPC_Response($v);
}
$r->headers = $this->xh[$pname]['headers'];
return $r;
}