[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/webservice/xmlrpc/ -> lib.php (source)

   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  /**
  19   * Moodle XML-RPC library
  20   *
  21   * @package    webservice_xmlrpc
  22   * @copyright  2009 Jerome Mouneyrac
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  /**
  27   * Moodle XML-RPC client
  28   *
  29   * It has been implemented for unit testing purpose (all protocols have similar client)
  30   *
  31   * @package    webservice_xmlrpc
  32   * @copyright  2010 Jerome Mouneyrac
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class webservice_xmlrpc_client {
  36  
  37      /** @var moodle_url The XML-RPC server url. */
  38      protected $serverurl;
  39  
  40      /** @var string The token for the XML-RPC call. */
  41      protected $token;
  42  
  43      /**
  44       * Constructor
  45       *
  46       * @param string $serverurl a Moodle URL
  47       * @param string $token the token used to do the web service call
  48       */
  49      public function __construct($serverurl, $token) {
  50          $this->serverurl = new moodle_url($serverurl);
  51          $this->token = $token;
  52      }
  53  
  54      /**
  55       * Set the token used to do the XML-RPC call
  56       *
  57       * @param string $token the token used to do the web service call
  58       */
  59      public function set_token($token) {
  60          $this->token = $token;
  61      }
  62  
  63      /**
  64       * Execute client WS request with token authentication
  65       *
  66       * @param string $functionname the function name
  67       * @param array $params An associative array containing the the parameters of the function being called.
  68       * @return mixed The decoded XML RPC response.
  69       * @throws moodle_exception
  70       */
  71      public function call($functionname, $params = array()) {
  72          if ($this->token) {
  73              $this->serverurl->param('wstoken', $this->token);
  74          }
  75  
  76          // Set output options.
  77          $outputoptions = array(
  78              'encoding' => 'utf-8'
  79          );
  80  
  81          // Encode the request.
  82          // See MDL-53962 - needed for backwards compatibility on <= 3.0
  83          $params = array_values($params);
  84          $request = xmlrpc_encode_request($functionname, $params, $outputoptions);
  85  
  86          // Set the headers.
  87          $headers = array(
  88              'Content-Length' => strlen($request),
  89              'Content-Type' => 'text/xml; charset=utf-8',
  90              'Host' => $this->serverurl->get_host(),
  91              'User-Agent' => 'Moodle XML-RPC Client/1.0',
  92          );
  93  
  94          // Get the response.
  95          $response = download_file_content($this->serverurl, $headers, $request);
  96  
  97          // Decode the response.
  98          $result = xmlrpc_decode($response);
  99          if (is_array($result) && xmlrpc_is_fault($result)) {
 100              throw new Exception($result['faultString'], $result['faultCode']);
 101          }
 102  
 103          return $result;
 104      }
 105  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1