2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Moodle XML-RPC library
20 * @package webservice_xmlrpc
21 * @copyright 2009 Jerome Mouneyrac
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
28 * Moodle XML-RPC client
30 * @package webservice_xmlrpc
31 * @copyright 2010 Jerome Mouneyrac
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class webservice_xmlrpc_client
{
36 /** @var moodle_url The XML-RPC server url. */
39 /** @var string The token for the XML-RPC call. */
45 * @param string $serverurl a Moodle URL
46 * @param string $token the token used to do the web service call
48 public function __construct($serverurl, $token) {
49 $this->serverurl
= new moodle_url($serverurl);
50 $this->token
= $token;
54 * Set the token used to do the XML-RPC call
56 * @param string $token the token used to do the web service call
58 public function set_token($token) {
59 $this->token
= $token;
63 * Execute client WS request with token authentication
65 * @param string $functionname the function name
66 * @param array $params An associative array containing the the parameters of the function being called.
67 * @return mixed The decoded XML RPC response.
68 * @throws moodle_exception
70 public function call($functionname, $params = array()) {
72 require_once($CFG->libdir
. '/filelib.php');
75 $this->serverurl
->param('wstoken', $this->token
);
78 $request = $this->encode_request($functionname, $params);
82 'Content-Length' => strlen($request),
83 'Content-Type' => 'text/xml; charset=utf-8',
84 'Host' => $this->serverurl
->get_host(),
85 'User-Agent' => 'Moodle XML-RPC Client/1.0',
89 $response = download_file_content($this->serverurl
->out(false), $headers, $request);
91 // Decode the response.
92 $result = $this->decode_response($response);
93 if (is_array($result) && xmlrpc_is_fault($result)) {
94 throw new Exception($result['faultString'], $result['faultCode']);
101 * Generates XML for a method request.
103 * @param string $functionname Name of the method to call.
104 * @param mixed $params Method parameters compatible with the method signature.
107 protected function encode_request($functionname, $params) {
109 $outputoptions = array(
110 'encoding' => 'utf-8',
111 'escaping' => 'markup',
114 // See MDL-53962 - needed for backwards compatibility on <= 3.0.
115 $params = array_values($params);
117 return xmlrpc_encode_request($functionname, $params, $outputoptions);
121 * Parses and decodes the response XML
123 * @param string $response
126 protected function decode_response($response) {
127 // XMLRPC server in Moodle encodes response using function xmlrpc_encode_request() with method==null
128 // see {@link webservice_xmlrpc_server::prepare_response()} . We should use xmlrpc_decode_request() for decoding too.
131 if (preg_match('/^<\?xml version="1.0" encoding="([^"]*)"\?>/', $response, $matches)) {
132 // Sometimes xmlrpc_decode_request() fails to recognise encoding, let's help it.
133 $encoding = $matches[1];
135 $r = xmlrpc_decode_request($response, $method, $encoding);