7 /// @todo: do these need to be public?
11 public $method = 'http';
15 public $username = '';
16 public $password = '';
19 public $certpass = '';
21 public $cacertdir = '';
24 public $verifypeer = true;
25 public $verifyhost = 2;
26 public $sslversion = 0; // corresponds to CURL_SSLVERSION_DEFAULT
27 public $no_multicall = false;
29 public $proxyport = 0;
30 public $proxy_user = '';
31 public $proxy_pass = '';
32 public $proxy_authtype = 1;
33 public $cookies = array();
34 public $extracurlopts = array();
37 * List of http compression methods accepted by the client for responses.
38 * NB: PHP supports deflate, gzip compressions out of the box if compiled w. zlib.
40 * NNB: you can set it to any non-empty array for HTTP11 and HTTPS, since
41 * in those cases it will be up to CURL to decide the compression methods
42 * it supports. You might check for the presence of 'zlib' in the output of
43 * curl_version() to determine wheter compression is supported or not
45 public $accepted_compression = array();
47 * Name of compression scheme to be used for sending requests.
48 * Either null, gzip or deflate.
50 public $request_compression = '';
52 * CURL handle: used for keep-alive connections (PHP 4.3.8 up, see:
53 * http://curl.haxx.se/docs/faq.html#7.3).
55 public $xmlrpc_curl_handle = null;
56 /// Whether to use persistent connections for http 1.1 and https
57 public $keepalive = false;
58 /// Charset encodings that can be decoded without problems by the client
59 public $accepted_charset_encodings = array();
60 /// Charset encoding to be used in serializing request. NULL = use ASCII
61 public $request_charset_encoding = '';
63 * Decides the content of Response objects returned by calls to send()
64 * valid strings are 'xmlrpcvals', 'phpvals' or 'xml'.
66 public $return_type = 'xmlrpcvals';
68 * Sent to servers in http headers.
73 * @param string $path either the complete server URL or the PATH part of the xmlrc server URL, e.g. /xmlrpc/server.php
74 * @param string $server the server name / ip address
75 * @param integer $port the port the server is listening on, defaults to 80 or 443 depending on protocol used
76 * @param string $method the http protocol variant: defaults to 'http', 'https' and 'http11' can be used if CURL is installed
78 public function __construct($path, $server = '', $port = '', $method = '')
80 // allow user to specify all params in $path
81 if ($server == '' and $port == '' and $method == '') {
82 $parts = parse_url($path);
83 $server = $parts['host'];
84 $path = isset($parts['path']) ? $parts['path'] : '';
85 if (isset($parts['query'])) {
86 $path .= '?' . $parts['query'];
88 if (isset($parts['fragment'])) {
89 $path .= '#' . $parts['fragment'];
91 if (isset($parts['port'])) {
92 $port = $parts['port'];
94 if (isset($parts['scheme'])) {
95 $method = $parts['scheme'];
97 if (isset($parts['user'])) {
98 $this->username = $parts['user'];
100 if (isset($parts['pass'])) {
101 $this->password = $parts['pass'];
104 if ($path == '' || $path[0] != '/') {
105 $this->path = '/' . $path;
109 $this->server = $server;
114 $this->method = $method;
117 // if ZLIB is enabled, let the client by default accept compressed responses
118 if (function_exists('gzinflate') || (
119 function_exists('curl_init') && (($info = curl_version()) &&
120 ((is_string($info) && strpos($info, 'zlib') !== null) || isset($info['libz_version'])))
123 $this->accepted_compression = array('gzip', 'deflate');
126 // keepalives: enabled by default
127 $this->keepalive = true;
129 // by default the xml parser can support these 3 charset encodings
130 $this->accepted_charset_encodings = array('UTF-8', 'ISO-8859-1', 'US-ASCII');
132 // initialize user_agent string
133 $this->user_agent = PhpXmlRpc::$xmlrpcName . ' ' . PhpXmlRpc::$xmlrpcVersion;
137 * Enables/disables the echoing to screen of the xmlrpc responses received.
139 * @param integer $in values 0, 1 and 2 are supported (2 = echo sent msg too, before received response)
141 public function setDebug($in)
147 * Add some http BASIC AUTH credentials, used by the client to authenticate.
149 * @param string $u username
150 * @param string $p password
151 * @param integer $t auth type. See curl_setopt man page for supported auth types. Defaults to CURLAUTH_BASIC (basic auth)
153 public function setCredentials($u, $p, $t = 1)
155 $this->username = $u;
156 $this->password = $p;
157 $this->authtype = $t;
161 * Add a client-side https certificate.
163 * @param string $cert
164 * @param string $certpass
166 public function setCertificate($cert, $certpass)
169 $this->certpass = $certpass;
173 * Add a CA certificate to verify server with (see man page about
174 * CURLOPT_CAINFO for more details).
176 * @param string $cacert certificate file name (or dir holding certificates)
177 * @param bool $is_dir set to true to indicate cacert is a dir. defaults to false
179 public function setCaCertificate($cacert, $is_dir = false)
182 $this->cacertdir = $cacert;
184 $this->cacert = $cacert;
189 * Set attributes for SSL communication: private SSL key
190 * NB: does not work in older php/curl installs
191 * Thanks to Daniel Convissor.
193 * @param string $key The name of a file containing a private SSL key
194 * @param string $keypass The secret password needed to use the private SSL key
196 public function setKey($key, $keypass)
199 $this->keypass = $keypass;
203 * Set attributes for SSL communication: verify server certificate.
205 * @param bool $i enable/disable verification of peer certificate
207 public function setSSLVerifyPeer($i)
209 $this->verifypeer = $i;
213 * Set attributes for SSL communication: verify match of server cert w. hostname.
217 public function setSSLVerifyHost($i)
219 $this->verifyhost = $i;
223 * Set attributes for SSL communication: SSL version to use. Best left at 0 (default value ): let cURL decide
227 public function setSSLVersion($i)
229 $this->sslversion = $i;
235 * @param string $proxyhost
236 * @param string $proxyport Defaults to 8080 for HTTP and 443 for HTTPS
237 * @param string $proxyusername Leave blank if proxy has public access
238 * @param string $proxypassword Leave blank if proxy has public access
239 * @param int $proxyauthtype set to constant CURLAUTH_NTLM to use NTLM auth with proxy
241 public function setProxy($proxyhost, $proxyport, $proxyusername = '', $proxypassword = '', $proxyauthtype = 1)
243 $this->proxy = $proxyhost;
244 $this->proxyport = $proxyport;
245 $this->proxy_user = $proxyusername;
246 $this->proxy_pass = $proxypassword;
247 $this->proxy_authtype = $proxyauthtype;
251 * Enables/disables reception of compressed xmlrpc responses.
252 * Note that enabling reception of compressed responses merely adds some standard
253 * http headers to xmlrpc requests. It is up to the xmlrpc server to return
254 * compressed responses when receiving such requests.
256 * @param string $compmethod either 'gzip', 'deflate', 'any' or ''
258 public function setAcceptedCompression($compmethod)
260 if ($compmethod == 'any') {
261 $this->accepted_compression = array('gzip', 'deflate');
262 } elseif ($compmethod == false) {
263 $this->accepted_compression = array();
265 $this->accepted_compression = array($compmethod);
270 * Enables/disables http compression of xmlrpc request.
271 * Take care when sending compressed requests: servers might not support them
272 * (and automatic fallback to uncompressed requests is not yet implemented).
274 * @param string $compmethod either 'gzip', 'deflate' or ''
276 public function setRequestCompression($compmethod)
278 $this->request_compression = $compmethod;
282 * Adds a cookie to list of cookies that will be sent to server.
283 * NB: setting any param but name and value will turn the cookie into a 'version 1' cookie:
284 * do not do it unless you know what you are doing.
286 * @param string $name
287 * @param string $value
288 * @param string $path
289 * @param string $domain
292 * @todo check correctness of urlencoding cookie value (copied from php way of doing it...)
294 public function setCookie($name, $value = '', $path = '', $domain = '', $port = null)
296 $this->cookies[$name]['value'] = urlencode($value);
297 if ($path || $domain || $port) {
298 $this->cookies[$name]['path'] = $path;
299 $this->cookies[$name]['domain'] = $domain;
300 $this->cookies[$name]['port'] = $port;
301 $this->cookies[$name]['version'] = 1;
303 $this->cookies[$name]['version'] = 0;
308 * Directly set cURL options, for extra flexibility
309 * It allows eg. to bind client to a specific IP interface / address.
311 * @param array $options
313 public function SetCurlOptions($options)
315 $this->extracurlopts = $options;
319 * Set user-agent string that will be used by this client instance
320 * in http headers sent to the server.
322 public function SetUserAgent($agentstring)
324 $this->user_agent = $agentstring;
328 * Send an xmlrpc request.
330 * @param Request|Request[]|string $msg The Request object, or an array of requests for using multicall, or the complete xml representation of a request
331 * @param integer $timeout Connection timeout, in seconds, If unspecified, a platform specific timeout will apply
332 * @param string $method if left unspecified, the http protocol chosen during creation of the object will be used
336 public function send($msg, $timeout = 0, $method = '')
338 // if user does not specify http protocol, use native method of this client
339 // (i.e. method set during call to constructor)
341 $method = $this->method;
344 if (is_array($msg)) {
345 // $msg is an array of Requests
346 $r = $this->multicall($msg, $timeout, $method);
349 } elseif (is_string($msg)) {
350 $n = new Request('');
355 // where msg is a Request
356 $msg->debug = $this->debug;
358 if ($method == 'https') {
359 $r = $this->sendPayloadHTTPS(
375 $this->proxy_authtype,
381 } elseif ($method == 'http11') {
382 $r = $this->sendPayloadCURL(
398 $this->proxy_authtype,
403 $r = $this->sendPayloadHTTP10(
415 $this->proxy_authtype
422 private function sendPayloadHTTP10($msg, $server, $port, $timeout = 0,
423 $username = '', $password = '', $authtype = 1, $proxyhost = '',
424 $proxyport = 0, $proxyusername = '', $proxypassword = '', $proxyauthtype = 1)
430 // Only create the payload if it was not created previously
431 if (empty($msg->payload)) {
432 $msg->createPayload($this->request_charset_encoding);
435 $payload = $msg->payload;
436 // Deflate request body and set appropriate request headers
437 if (function_exists('gzdeflate') && ($this->request_compression == 'gzip' || $this->request_compression == 'deflate')) {
438 if ($this->request_compression == 'gzip') {
439 $a = @gzencode($payload);
442 $encoding_hdr = "Content-Encoding: gzip\r\n";
445 $a = @gzcompress($payload);
448 $encoding_hdr = "Content-Encoding: deflate\r\n";
455 // thanks to Grant Rauscher <grant7@firstworld.net> for this
457 if ($username != '') {
458 $credentials = 'Authorization: Basic ' . base64_encode($username . ':' . $password) . "\r\n";
459 if ($authtype != 1) {
460 error_log('XML-RPC: ' . __METHOD__ . ': warning. Only Basic auth is supported with HTTP 1.0');
464 $accepted_encoding = '';
465 if (is_array($this->accepted_compression) && count($this->accepted_compression)) {
466 $accepted_encoding = 'Accept-Encoding: ' . implode(', ', $this->accepted_compression) . "\r\n";
469 $proxy_credentials = '';
471 if ($proxyport == 0) {
474 $connectserver = $proxyhost;
475 $connectport = $proxyport;
476 $uri = 'http://' . $server . ':' . $port . $this->path;
477 if ($proxyusername != '') {
478 if ($proxyauthtype != 1) {
479 error_log('XML-RPC: ' . __METHOD__ . ': warning. Only Basic auth to proxy is supported with HTTP 1.0');
481 $proxy_credentials = 'Proxy-Authorization: Basic ' . base64_encode($proxyusername . ':' . $proxypassword) . "\r\n";
484 $connectserver = $server;
485 $connectport = $port;
489 // Cookie generation, as per rfc2965 (version 1 cookies) or
490 // netscape's rules (version 0 cookies)
492 if (count($this->cookies)) {
494 foreach ($this->cookies as $name => $cookie) {
495 if ($cookie['version']) {
496 $version = ' $Version="' . $cookie['version'] . '";';
497 $cookieheader .= ' ' . $name . '="' . $cookie['value'] . '";';
498 if ($cookie['path']) {
499 $cookieheader .= ' $Path="' . $cookie['path'] . '";';
501 if ($cookie['domain']) {
502 $cookieheader .= ' $Domain="' . $cookie['domain'] . '";';
504 if ($cookie['port']) {
505 $cookieheader .= ' $Port="' . $cookie['port'] . '";';
508 $cookieheader .= ' ' . $name . '=' . $cookie['value'] . ";";
511 $cookieheader = 'Cookie:' . $version . substr($cookieheader, 0, -1) . "\r\n";
515 $port = ($port == 80) ? '' : (':' . $port);
517 $op = 'POST ' . $uri . " HTTP/1.0\r\n" .
518 'User-Agent: ' . $this->user_agent . "\r\n" .
519 'Host: ' . $server . $port . "\r\n" .
524 'Accept-Charset: ' . implode(',', $this->accepted_charset_encodings) . "\r\n" .
526 'Content-Type: ' . $msg->content_type . "\r\nContent-Length: " .
527 strlen($payload) . "\r\n\r\n" .
530 if ($this->debug > 1) {
531 $this->debugMessage("---SENDING---\n$op\n---END---");
535 $fp = @fsockopen($connectserver, $connectport, $this->errno, $this->errstr, $timeout);
537 $fp = @fsockopen($connectserver, $connectport, $this->errno, $this->errstr);
540 if ($timeout > 0 && function_exists('stream_set_timeout')) {
541 stream_set_timeout($fp, $timeout);
544 $this->errstr = 'Connect error: ' . $this->errstr;
545 $r = new Response(0, PhpXmlRpc::$xmlrpcerr['http_error'], $this->errstr . ' (' . $this->errno . ')');
550 if (!fputs($fp, $op, strlen($op))) {
552 $this->errstr = 'Write error';
553 $r = new Response(0, PhpXmlRpc::$xmlrpcerr['http_error'], $this->errstr);
557 // reset errno and errstr on successful socket connection
560 // G. Giunta 2005/10/24: close socket before parsing.
561 // should yield slightly better execution times, and make easier recursive calls (e.g. to follow http redirects)
564 // shall we check for $data === FALSE?
565 // as per the manual, it signals an error
566 $ipd .= fread($fp, 32768);
567 } while (!feof($fp));
569 $r = $msg->parseResponse($ipd, false, $this->return_type);
574 private function sendPayloadHTTPS($msg, $server, $port, $timeout = 0, $username = '',
575 $password = '', $authtype = 1, $cert = '', $certpass = '', $cacert = '', $cacertdir = '',
576 $proxyhost = '', $proxyport = 0, $proxyusername = '', $proxypassword = '', $proxyauthtype = 1,
577 $keepalive = false, $key = '', $keypass = '', $sslversion = 0)
579 $r = $this->sendPayloadCURL($msg, $server, $port, $timeout, $username,
580 $password, $authtype, $cert, $certpass, $cacert, $cacertdir, $proxyhost, $proxyport,
581 $proxyusername, $proxypassword, $proxyauthtype, 'https', $keepalive, $key, $keypass, $sslversion);
587 * Contributed by Justin Miller <justin@voxel.net>
588 * Requires curl to be built into PHP
589 * NB: CURL versions before 7.11.10 cannot use proxy to talk to https servers!
591 private function sendPayloadCURL($msg, $server, $port, $timeout = 0, $username = '',
592 $password = '', $authtype = 1, $cert = '', $certpass = '', $cacert = '', $cacertdir = '',
593 $proxyhost = '', $proxyport = 0, $proxyusername = '', $proxypassword = '', $proxyauthtype = 1, $method = 'https',
594 $keepalive = false, $key = '', $keypass = '', $sslversion = 0)
596 if (!function_exists('curl_init')) {
597 $this->errstr = 'CURL unavailable on this install';
598 $r = new Response(0, PhpXmlRpc::$xmlrpcerr['no_curl'], PhpXmlRpc::$xmlrpcstr['no_curl']);
602 if ($method == 'https') {
603 if (($info = curl_version()) &&
604 ((is_string($info) && strpos($info, 'OpenSSL') === null) || (is_array($info) && !isset($info['ssl_version'])))
606 $this->errstr = 'SSL unavailable on this install';
607 $r = new Response(0, PhpXmlRpc::$xmlrpcerr['no_ssl'], PhpXmlRpc::$xmlrpcstr['no_ssl']);
614 if ($method == 'http') {
621 // Only create the payload if it was not created previously
622 if (empty($msg->payload)) {
623 $msg->createPayload($this->request_charset_encoding);
626 // Deflate request body and set appropriate request headers
627 $payload = $msg->payload;
628 if (function_exists('gzdeflate') && ($this->request_compression == 'gzip' || $this->request_compression == 'deflate')) {
629 if ($this->request_compression == 'gzip') {
630 $a = @gzencode($payload);
633 $encoding_hdr = 'Content-Encoding: gzip';
636 $a = @gzcompress($payload);
639 $encoding_hdr = 'Content-Encoding: deflate';
646 if ($this->debug > 1) {
647 $this->debugMessage("---SENDING---\n$payload\n---END---");
648 // let the client see this now in case http times out...
652 if (!$keepalive || !$this->xmlrpc_curl_handle) {
653 $curl = curl_init($method . '://' . $server . ':' . $port . $this->path);
655 $this->xmlrpc_curl_handle = $curl;
658 $curl = $this->xmlrpc_curl_handle;
661 // results into variable
662 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
665 curl_setopt($curl, CURLOPT_VERBOSE, 1);
667 curl_setopt($curl, CURLOPT_USERAGENT, $this->user_agent);
668 // required for XMLRPC: post the data
669 curl_setopt($curl, CURLOPT_POST, 1);
671 curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
673 // return the header too
674 curl_setopt($curl, CURLOPT_HEADER, 1);
676 // NB: if we set an empty string, CURL will add http header indicating
677 // ALL methods it is supporting. This is possibly a better option than
678 // letting the user tell what curl can / cannot do...
679 if (is_array($this->accepted_compression) && count($this->accepted_compression)) {
680 //curl_setopt($curl, CURLOPT_ENCODING, implode(',', $this->accepted_compression));
681 // empty string means 'any supported by CURL' (shall we catch errors in case CURLOPT_SSLKEY undefined ?)
682 if (count($this->accepted_compression) == 1) {
683 curl_setopt($curl, CURLOPT_ENCODING, $this->accepted_compression[0]);
685 curl_setopt($curl, CURLOPT_ENCODING, '');
689 $headers = array('Content-Type: ' . $msg->content_type, 'Accept-Charset: ' . implode(',', $this->accepted_charset_encodings));
690 // if no keepalive is wanted, let the server know it in advance
692 $headers[] = 'Connection: close';
694 // request compression header
696 $headers[] = $encoding_hdr;
699 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
702 curl_setopt($curl, CURLOPT_TIMEOUT, $timeout == 1 ? 1 : $timeout - 1);
705 if ($username && $password) {
706 curl_setopt($curl, CURLOPT_USERPWD, $username . ':' . $password);
707 if (defined('CURLOPT_HTTPAUTH')) {
708 curl_setopt($curl, CURLOPT_HTTPAUTH, $authtype);
709 } elseif ($authtype != 1) {
710 error_log('XML-RPC: ' . __METHOD__ . ': warning. Only Basic auth is supported by the current PHP/curl install');
714 if ($method == 'https') {
717 curl_setopt($curl, CURLOPT_SSLCERT, $cert);
721 curl_setopt($curl, CURLOPT_SSLCERTPASSWD, $certpass);
723 // whether to verify remote host's cert
724 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $this->verifypeer);
725 // set ca certificates file/dir
727 curl_setopt($curl, CURLOPT_CAINFO, $cacert);
730 curl_setopt($curl, CURLOPT_CAPATH, $cacertdir);
732 // set key file (shall we catch errors in case CURLOPT_SSLKEY undefined ?)
734 curl_setopt($curl, CURLOPT_SSLKEY, $key);
736 // set key password (shall we catch errors in case CURLOPT_SSLKEY undefined ?)
738 curl_setopt($curl, CURLOPT_SSLKEYPASSWD, $keypass);
740 // whether to verify cert's common name (CN); 0 for no, 1 to verify that it exists, and 2 to verify that it matches the hostname used
741 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, $this->verifyhost);
742 // allow usage of different SSL versions
743 curl_setopt($curl, CURLOPT_SSLVERSION, $sslversion);
748 if ($proxyport == 0) {
749 $proxyport = 8080; // NB: even for HTTPS, local connection is on port 8080
751 curl_setopt($curl, CURLOPT_PROXY, $proxyhost . ':' . $proxyport);
752 //curl_setopt($curl, CURLOPT_PROXYPORT,$proxyport);
753 if ($proxyusername) {
754 curl_setopt($curl, CURLOPT_PROXYUSERPWD, $proxyusername . ':' . $proxypassword);
755 if (defined('CURLOPT_PROXYAUTH')) {
756 curl_setopt($curl, CURLOPT_PROXYAUTH, $proxyauthtype);
757 } elseif ($proxyauthtype != 1) {
758 error_log('XML-RPC: ' . __METHOD__ . ': warning. Only Basic auth to proxy is supported by the current PHP/curl install');
763 // NB: should we build cookie http headers by hand rather than let CURL do it?
764 // the following code does not honour 'expires', 'path' and 'domain' cookie attributes
765 // set to client obj the the user...
766 if (count($this->cookies)) {
768 foreach ($this->cookies as $name => $cookie) {
769 $cookieheader .= $name . '=' . $cookie['value'] . '; ';
771 curl_setopt($curl, CURLOPT_COOKIE, substr($cookieheader, 0, -2));
774 foreach ($this->extracurlopts as $opt => $val) {
775 curl_setopt($curl, $opt, $val);
778 $result = curl_exec($curl);
780 if ($this->debug > 1) {
781 $message = "---CURL INFO---\n";
782 foreach (curl_getinfo($curl) as $name => $val) {
783 if (is_array($val)) {
784 $val = implode("\n", $val);
786 $message .= $name . ': ' . $val . "\n";
788 $message .= "---END---";
789 $this->debugMessage($message);
793 /// @todo we should use a better check here - what if we get back '' or '0'?
795 $this->errstr = 'no response';
796 $resp = new Response(0, PhpXmlRpc::$xmlrpcerr['curl_fail'], PhpXmlRpc::$xmlrpcstr['curl_fail'] . ': ' . curl_error($curl));
799 $this->xmlrpc_curl_handle = null;
805 $resp = $msg->parseResponse($result, true, $this->return_type);
806 // if we got back a 302, we can not reuse the curl handle for later calls
807 if ($resp->faultCode() == PhpXmlRpc::$xmlrpcerr['http_error'] && $keepalive) {
809 $this->xmlrpc_curl_handle = null;
817 * Send an array of requests and return an array of responses.
818 * Unless $this->no_multicall has been set to true, it will try first
819 * to use one single xmlrpc call to server method system.multicall, and
820 * revert to sending many successive calls in case of failure.
821 * This failure is also stored in $this->no_multicall for subsequent calls.
822 * Unfortunately, there is no server error code universally used to denote
823 * the fact that multicall is unsupported, so there is no way to reliably
824 * distinguish between that and a temporary failure.
825 * If you are sure that server supports multicall and do not want to
826 * fallback to using many single calls, set the fourth parameter to FALSE.
828 * NB: trying to shoehorn extra functionality into existing syntax has resulted
829 * in pretty much convoluted code...
831 * @param Request[] $msgs an array of Request objects
832 * @param integer $timeout connection timeout (in seconds)
833 * @param string $method the http protocol variant to be used
834 * @param boolean fallback When true, upon receiving an error during multicall, multiple single calls will be attempted
838 public function multicall($msgs, $timeout = 0, $method = '', $fallback = true)
841 $method = $this->method;
843 if (!$this->no_multicall) {
844 $results = $this->_try_multicall($msgs, $timeout, $method);
845 if (is_array($results)) {
846 // System.multicall succeeded
849 // either system.multicall is unsupported by server,
850 // or call failed for some other reason.
852 // Don't try it next time...
853 $this->no_multicall = true;
855 if (is_a($results, '\PhpXmlRpc\Response')) {
858 $result = new Response(0, PhpXmlRpc::$xmlrpcerr['multicall_error'], PhpXmlRpc::$xmlrpcstr['multicall_error']);
863 // override fallback, in case careless user tries to do two
864 // opposite things at the same time
870 // system.multicall is (probably) unsupported by server:
871 // emulate multicall via multiple requests
872 foreach ($msgs as $msg) {
873 $results[] = $this->send($msg, $timeout, $method);
876 // user does NOT want to fallback on many single calls:
877 // since we should always return an array of responses,
878 // return an array with the same error repeated n times
879 foreach ($msgs as $msg) {
880 $results[] = $result;
888 * Attempt to boxcar $msgs via system.multicall.
889 * Returns either an array of xmlrpcreponses, an xmlrpc error response
890 * or false (when received response does not respect valid multicall syntax).
892 private function _try_multicall($msgs, $timeout, $method)
894 // Construct multicall request
896 foreach ($msgs as $msg) {
897 $call['methodName'] = new Value($msg->method(), 'string');
898 $numParams = $msg->getNumParams();
900 for ($i = 0; $i < $numParams; $i++) {
901 $params[$i] = $msg->getParam($i);
903 $call['params'] = new Value($params, 'array');
904 $calls[] = new Value($call, 'struct');
906 $multicall = new Request('system.multicall');
907 $multicall->addParam(new Value($calls, 'array'));
910 $result = $this->send($multicall, $timeout, $method);
912 if ($result->faultCode() != 0) {
913 // call to system.multicall failed
918 $rets = $result->value();
920 if ($this->return_type == 'xml') {
922 } elseif ($this->return_type == 'phpvals') {
923 ///@todo test this code branch...
924 $rets = $result->value();
925 if (!is_array($rets)) {
926 return false; // bad return type from system.multicall
928 $numRets = count($rets);
929 if ($numRets != count($msgs)) {
930 return false; // wrong number of return values.
934 for ($i = 0; $i < $numRets; $i++) {
936 if (!is_array($val)) {
939 switch (count($val)) {
941 if (!isset($val[0])) {
942 return false; // Bad value
944 // Normal return value
945 $response[$i] = new Response($val[0], 0, '', 'phpvals');
948 /// @todo remove usage of @: it is apparently quite slow
949 $code = @$val['faultCode'];
950 if (!is_int($code)) {
953 $str = @$val['faultString'];
954 if (!is_string($str)) {
957 $response[$i] = new Response(0, $code, $str);
966 // return type == 'xmlrpcvals'
968 $rets = $result->value();
969 if ($rets->kindOf() != 'array') {
970 return false; // bad return type from system.multicall
972 $numRets = $rets->arraysize();
973 if ($numRets != count($msgs)) {
974 return false; // wrong number of return values.
978 for ($i = 0; $i < $numRets; $i++) {
979 $val = $rets->arraymem($i);
980 switch ($val->kindOf()) {
982 if ($val->arraysize() != 1) {
983 return false; // Bad value
985 // Normal return value
986 $response[$i] = new Response($val->arraymem(0));
989 $code = $val->structmem('faultCode');
990 if ($code->kindOf() != 'scalar' || $code->scalartyp() != 'int') {
993 $str = $val->structmem('faultString');
994 if ($str->kindOf() != 'scalar' || $str->scalartyp() != 'string') {
997 $response[$i] = new Response(0, $code->scalarval(), $str->scalarval());
1009 * Echoes a debug message, taking care of escaping it when not in console mode
1011 * @param string $message
1013 protected function debugMessage($message)
1015 if (PHP_SAPI != 'cli') {
1016 print "<PRE>\n".htmlentities($message)."\n</PRE>";
1019 print "\n$message\n";
1021 // let the client see this now in case http times out...