Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/webservice/rest/lib.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00022 class webservice_rest_client {
00023 
00025     private $serverurl;
00026 
00027     private $token;
00028     private $format;
00029 
00035     public function __construct($serverurl, $token, $format = 'xml') {
00036         $this->serverurl = new moodle_url($serverurl);
00037         $this->token = $token;
00038         $this->format = $format;
00039     }
00040 
00045     public function set_token($token) {
00046         $this->token = $token;
00047     }
00048 
00055     public function call($functionname, $params) {
00056         global $DB, $CFG;
00057 
00058          if ($this->format == 'json') {
00059              $formatparam = '&moodlewsrestformat=json';
00060              $this->serverurl->param('moodlewsrestformat','json');
00061          } else {
00062              $formatparam = ''; //to keep retro compability with old server that only support xml (they don't expect this param)
00063          }
00064 
00065         $this->serverurl->param('wstoken',$this->token);
00066         $this->serverurl->param('wsfunction',$functionname); //you could also use params().
00067 
00068         $result = download_file_content($this->serverurl->out(false), null, $params);
00069 
00070         //TODO : transform the XML result into PHP values - MDL-22965
00071         if ($this->format == 'json') {
00072             $result = json_decode($result);
00073         }
00074 
00075         return $result;
00076     }
00077 
00078 }
 All Data Structures Namespaces Files Functions Variables Enumerations