Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 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: 284 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)

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