Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   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.
  13  //
  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/>.
  16  
  17  /**
  18   * Moodle SOAP library
  19   *
  20   * @package    webservice_soap
  21   * @copyright  2009 Jerome Mouneyrac
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  /**
  26   * Moodle SOAP client
  27   *
  28   * It has been implemented for unit testing purpose (all protocols have similar client)
  29   *
  30   * @package    webservice_soap
  31   * @copyright  2010 Jerome Mouneyrac
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class webservice_soap_client {
  35  
  36      /** @var moodle_url The server url. */
  37      private $serverurl;
  38  
  39      /** @var  string The WS token. */
  40      private $token;
  41  
  42      /** @var array|null SOAP options. */
  43      private $options;
  44  
  45      /**
  46       * Constructor
  47       *
  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
  51       */
  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();
  56      }
  57  
  58      /**
  59       * Set the token used to do the SOAP call
  60       *
  61       * @param string $token the token used to do the web service call
  62       */
  63      public function set_token($token) {
  64          $this->token = $token;
  65      }
  66  
  67      /**
  68       * Execute client WS request with token authentication
  69       *
  70       * @param string $functionname the function name
  71       * @param array $params the parameters of the function
  72       * @return mixed
  73       */
  74      public function call($functionname, $params) {
  75          if ($this->token) {
  76              $this->serverurl->param('wstoken', $this->token);
  77          }
  78          $this->serverurl->param('wsdl', 1);
  79  
  80          $opts = array(
  81              'http' => array(
  82                  'user_agent' => 'Moodle SOAP Client'
  83              )
  84          );
  85          $context = stream_context_create($opts);
  86          $this->options['stream_context'] = $context;
  87          $this->options['cache_wsdl'] = WSDL_CACHE_NONE;
  88  
  89          $client = new SoapClient($this->serverurl->out(false), $this->options);
  90  
  91          return $client->__soapCall($functionname, $params);
  92      }
  93  }