Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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.

Differences Between: [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

REST web service implementation classes and methods.

Copyright: 2009 Jerome Mouneyrac
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 268 lines (10 kb)
Included or required:0 times
Referenced: 1 time
Includes or requires: 0 files

Defines 2 classes

webservice_rest_server:: (8 methods):
  __construct()
  set_rest_format()
  parse_request()
  send_response()
  send_error()
  generate_error()
  send_headers()
  xmlize_result()

webservice_rest_test_client:: (1 method):
  simpletest()


Class: webservice_rest_server  - X-Ref

REST service server implementation.

__construct($authmethod)   X-Ref
Contructor

param: string $authmethod authentication method of the web service (WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN, ...)
param: string $restformat Format of the return values: 'xml' or 'json'

set_rest_format()   X-Ref
Set the request format to.


parse_request()   X-Ref
This method parses the $_POST and $_GET superglobals and looks for
the following information:
1/ user authentication - username+password or token (wsusername, wspassword and wstoken parameters)
2/ function name (wsfunction parameter)
3/ function parameters (all other parameters except those above)
4/ text format parameters
5/ return rest format xml/json


send_response()   X-Ref
Send the result of function call to the WS client
formatted as XML document.


send_error($ex=null)   X-Ref
Send the error information to the WS client
formatted as XML document.
Note: the exception is never passed as null,
it only matches the abstract function declaration.

param: exception $ex the exception that we are sending

generate_error($ex)   X-Ref
Build the error information matching the REST returned value format (JSON or XML)

param: exception $ex the exception we are converting in the server rest format
return: string the error in the requested REST format

send_headers()   X-Ref
Internal implementation - sending of page headers.


xmlize_result($returns, $desc)   X-Ref
Internal implementation - recursive function producing XML markup.

param: mixed $returns the returned values
param: external_description $desc
return: string

Class: webservice_rest_test_client  - X-Ref

REST test client class

simpletest($serverurl, $function, $params)   X-Ref
Execute test client WS request

param: string $serverurl server url (including token parameter or username/password parameters)
param: string $function function name
param: array $params parameters of the called function
return: mixed