Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 402] [Versions 400 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: 277 lines (11 kb)
Included or required:0 times
Referenced: 0 times
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)

return: string the error in the requested REST format
param: exception $ex the exception we are converting in the server 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.

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

Class: webservice_rest_test_client  - X-Ref

REST test client class

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

return: mixed
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