Merge branch 'MDL-62768-37' of https://github.com/paulholden/moodle into MOODLE_37_STABLE
[moodle.git] / webservice / rest / lib.php
blob5a79ded6cece6a84f9f32dc4d01e1a59bbb19736
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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 /**
19 * Moodle REST library
21 * @package webservice_rest
22 * @copyright 2009 Jerome Mouneyrac
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 /**
28 * Moodle REST client
30 * It has been implemented for unit testing purpose (all protocols have similar client)
32 * @package webservice_rest
33 * @copyright 2010 Jerome Mouneyrac
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class webservice_rest_client {
38 /** @var moodle_url the REST server url */
39 private $serverurl;
41 /** @var string token */
42 private $token;
44 /** @var string returned value format: xml or json */
45 private $format;
47 /**
48 * Constructor
50 * @param string $serverurl a Moodle URL
51 * @param string $token the token used to do the web service call
52 * @param string $format returned value format: xml or json
54 public function __construct($serverurl, $token, $format = 'xml') {
55 $this->serverurl = new moodle_url($serverurl);
56 $this->token = $token;
57 $this->format = $format;
60 /**
61 * Set the token used to do the REST call
63 * @param string $token the token used to do the web service call
65 public function set_token($token) {
66 $this->token = $token;
69 /**
70 * Execute client WS request with token authentication
72 * @param string $functionname the function name
73 * @param array $params the parameters of the function
74 * @return mixed
76 public function call($functionname, $params) {
77 global $DB, $CFG;
79 if ($this->format == 'json') {
80 $formatparam = '&moodlewsrestformat=json';
81 $this->serverurl->param('moodlewsrestformat','json');
82 } else {
83 $formatparam = ''; //to keep retro compability with old server that only support xml (they don't expect this param)
86 $this->serverurl->param('wstoken',$this->token);
87 $this->serverurl->param('wsfunction',$functionname); //you could also use params().
89 $result = download_file_content($this->serverurl->out(false), null, $params);
91 //TODO MDL-22965 transform the XML result into PHP values
92 if ($this->format == 'json') {
93 $result = json_decode($result);
96 return $result;