15 public $params = array();
17 public $content_type =
'text/xml';
20 protected $httpResponse = array();
28 $this->methodname = $methodName;
29 foreach ($params as $param) {
30 $this->addParam($param);
36 if ($charsetEncoding !=
'') {
37 return "<?xml version=\"1.0\" encoding=\"$charsetEncoding\" ?" .
">\n<methodCall>\n";
39 return "<?xml version=\"1.0\"?" .
">\n<methodCall>\n";
45 return '</methodCall>';
50 if ($charsetEncoding !=
'') {
51 $this->content_type =
'text/xml; charset=' . $charsetEncoding;
53 $this->content_type =
'text/xml';
55 $this->payload = $this->xml_header($charsetEncoding);
56 $this->payload .=
'<methodName>' . Charset::instance()->encodeEntities($this->methodname, PhpXmlRpc::$xmlrpc_internalencoding, $charsetEncoding) .
"</methodName>\n";
57 $this->payload .=
"<params>\n";
58 foreach ($this->params as $p) {
59 $this->payload .=
"<param>\n" . $p->serialize($charsetEncoding) .
62 $this->payload .=
"</params>\n";
63 $this->payload .= $this->xml_footer();
73 public function method($methodName =
'')
75 if ($methodName !=
'') {
76 $this->methodname = $methodName;
79 return $this->methodname;
91 $this->createPayload($charsetEncoding);
93 return $this->payload;
106 if (is_object($param) && is_a($param,
'PhpXmlRpc\Value')) {
107 $this->params[] = $param;
124 return $this->params[$i];
134 return count($this->params);
155 while ($data = fread($fp, 32768)) {
158 return $this->parseResponse($ipd);
170 public function parseResponse($data =
'', $headersProcessed =
false, $returnType =
'xmlrpcvals')
173 Logger::instance()->debugMessage(
"---GOT---\n$data\n---END---");
176 $this->httpResponse = array(
'raw_data' => $data,
'headers' => array(),
'cookies' => array());
179 error_log(
'XML-RPC: ' . __METHOD__ .
': no response received from server.');
180 return new Response(0, PhpXmlRpc::$xmlrpcerr[
'no_data'], PhpXmlRpc::$xmlrpcstr[
'no_data']);
184 if (substr($data, 0, 4) ==
'HTTP') {
185 $httpParser =
new Http();
187 $this->httpResponse = $httpParser->parseResponseHeaders($data, $headersProcessed, $this->debug);
188 }
catch(\Exception $e) {
189 $r =
new Response(0, $e->getCode(), $e->getMessage());
192 $r->raw_data = $data;
205 $pos = strrpos($data,
'</methodResponse>');
206 if ($pos !==
false) {
207 $data = substr($data, 0, $pos + 17);
211 $respEncoding = XMLParser::guessEncoding(@$this->httpResponse[
'headers'][
'content-type'], $data);
214 $start = strpos($data,
'<!-- SERVER DEBUG INFO (BASE64 ENCODED):');
216 $start += strlen(
'<!-- SERVER DEBUG INFO (BASE64 ENCODED):');
217 $end = strpos($data,
'-->', $start);
218 $comments = substr($data, $start, $end - $start);
219 Logger::instance()->debugMessage(
"---SERVER DEBUG INFO (DECODED) ---\n\t" .
220 str_replace(
"\n",
"\n\t", base64_decode($comments)) .
"\n---END---", $respEncoding);
225 if ($returnType ==
'xml') {
226 $r =
new Response($data, 0,
'',
'xml');
227 $r->hdrs = $this->httpResponse[
'headers'];
228 $r->_cookies = $this->httpResponse[
'cookies'];
229 $r->raw_data = $this->httpResponse[
'raw_data'];
234 if ($respEncoding !=
'') {
241 if (!in_array($respEncoding, array(
'UTF-8',
'US-ASCII')) && !XMLParser::hasEncoding($data)) {
242 if ($respEncoding ==
'ISO-8859-1') {
243 $data = utf8_encode($data);
245 if (extension_loaded(
'mbstring')) {
246 $data = mb_convert_encoding($data,
'UTF-8', $respEncoding);
248 error_log(
'XML-RPC: ' . __METHOD__ .
': invalid charset encoding of received response: ' . $respEncoding);
254 $parser = xml_parser_create();
255 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING,
true);
262 if (!in_array(PhpXmlRpc::$xmlrpc_internalencoding, array(
'UTF-8',
'ISO-8859-1',
'US-ASCII'))) {
263 xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING,
'UTF-8');
265 xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, PhpXmlRpc::$xmlrpc_internalencoding);
269 xml_set_object($parser, $xmlRpcParser);
271 if ($returnType ==
'phpvals') {
272 xml_set_element_handler($parser,
'xmlrpc_se',
'xmlrpc_ee_fast');
274 xml_set_element_handler($parser,
'xmlrpc_se',
'xmlrpc_ee');
277 xml_set_character_data_handler($parser,
'xmlrpc_cd');
278 xml_set_default_handler($parser,
'xmlrpc_dh');
281 if (!xml_parse($parser, $data, count($data))) {
283 if ((xml_get_current_line_number($parser)) == 1) {
284 $errStr =
'XML error at line 1, check URL';
286 $errStr = sprintf(
'XML error: %s at line %d, column %d',
287 xml_error_string(xml_get_error_code($parser)),
288 xml_get_current_line_number($parser), xml_get_current_column_number($parser));
291 $r =
new Response(0, PhpXmlRpc::$xmlrpcerr[
'invalid_return'], PhpXmlRpc::$xmlrpcstr[
'invalid_return'] .
' (' . $errStr .
')');
292 xml_parser_free($parser);
296 $r->hdrs = $this->httpResponse[
'headers'];
297 $r->_cookies = $this->httpResponse[
'cookies'];
298 $r->raw_data = $this->httpResponse[
'raw_data'];
302 xml_parser_free($parser);
304 if ($xmlRpcParser->_xh[
'isf'] > 1) {
309 $r =
new Response(0, PhpXmlRpc::$xmlrpcerr[
'invalid_return'],
310 PhpXmlRpc::$xmlrpcstr[
'invalid_return'] .
' ' . $xmlRpcParser->_xh[
'isf_reason']);
314 elseif ($returnType ==
'xmlrpcvals' && !is_object($xmlRpcParser->_xh[
'value'])) {
318 $r =
new Response(0, PhpXmlRpc::$xmlrpcerr[
'invalid_return'],
319 PhpXmlRpc::$xmlrpcstr[
'invalid_return']);
321 if ($this->debug > 1) {
322 Logger::instance()->debugMessage(
323 "---PARSED---\n".var_export($xmlRpcParser->_xh[
'value'],
true).
"\n---END---"
328 $v = &$xmlRpcParser->_xh[
'value'];
330 if ($xmlRpcParser->_xh[
'isf']) {
332 if ($returnType ==
'xmlrpcvals') {
333 $errNo_v = $v[
'faultCode'];
334 $errStr_v = $v[
'faultString'];
335 $errNo = $errNo_v->scalarval();
336 $errStr = $errStr_v->scalarval();
338 $errNo = $v[
'faultCode'];
339 $errStr = $v[
'faultString'];
347 $r =
new Response(0, $errNo, $errStr);
349 $r =
new Response($v, 0,
'', $returnType);
353 $r->hdrs = $this->httpResponse[
'headers'];
354 $r->_cookies = $this->httpResponse[
'cookies'];
355 $r->raw_data = $this->httpResponse[
'raw_data'];