Automatically generated installer lang files
[moodle.git] / webservice / soap / lib.php
blob65d7d51fe59c3eedcc129b5c829ab82485e4e79e
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/>.
17 /**
18 * Moodle SOAP library
20 * @package webservice_soap
21 * @copyright 2009 Jerome Mouneyrac
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 /**
26 * Moodle SOAP client
28 * It has been implemented for unit testing purpose (all protocols have similar client)
30 * @package webservice_soap
31 * @copyright 2010 Jerome Mouneyrac
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 class webservice_soap_client {
36 /** @var moodle_url The server url. */
37 private $serverurl;
39 /** @var string The WS token. */
40 private $token;
42 /** @var array|null SOAP options. */
43 private $options;
45 /**
46 * Constructor
48 * @param string $serverurl a Moodle URL
49 * @param string $token the token used to do the web service call
50 * @param array $options PHP SOAP client options - see php.net
52 public function __construct($serverurl, $token = null, array $options = null) {
53 $this->serverurl = new moodle_url($serverurl);
54 $this->token = $token ?: $this->serverurl->get_param('wstoken');
55 $this->options = $options ?: array();
58 /**
59 * Set the token used to do the SOAP call
61 * @param string $token the token used to do the web service call
63 public function set_token($token) {
64 $this->token = $token;
67 /**
68 * Execute client WS request with token authentication
70 * @param string $functionname the function name
71 * @param array $params the parameters of the function
72 * @return mixed
74 public function call($functionname, $params) {
75 if ($this->token) {
76 $this->serverurl->param('wstoken', $this->token);
78 $this->serverurl->param('wsdl', 1);
80 $opts = array(
81 'http' => array(
82 'user_agent' => 'Moodle SOAP Client'
85 $context = stream_context_create($opts);
86 $this->options['stream_context'] = $context;
87 $this->options['cache_wsdl'] = WSDL_CACHE_NONE;
89 $client = new SoapClient($this->serverurl->out(false), $this->options);
91 return $client->__soapCall($functionname, $params);