Web service functions - C0D3D3V/Moodle-DL GitHub Wiki

In the official list of all API functions, you will find all functions that can be called via the web service. Because the functions differ from version to version, there is no good, official documentation. The functions relevant for this downloader are described in detail in other articles.

In general, each function works in the same way:

We send a post-request to https://your.site.com/moodle/webservice/rest/server.php?wsfunction=$wsfunction&moodlewsrestformat=json where $wsfunction is the function we want to call.

The post data must contain at least the following data:

'moodlewssettingfilter': 'true',
'moodlewssettingfileurl': 'true',
'wsfunction': $wsfunction,
'wstoken': $token

moodlewssettingfilter and moodlewssettingfileurl is only sent because that's what the Moodle app does. See the documentation:

  • moodlewssettingfileurl => true by default, returned file urls are converted to 'http://xxxx/webservice/pluginfile.php/yyyyyyyy'. If false the raw file url content from the DB is returned (e.g. @@PLUGINFILE@@)
  • moodlewssettingfilter => false by default. If true, the function will filter during format_text()

$token is the token for authentication of the user.

Almost all functions require extra parameters or offer optional parameters. To find out what parameters a function needs, there is nothing left to do but search the source code of Moodle. In the source code, each interface is documented and all parameters are explained individually. Here are two examples: Example 1 and example 2. The services.php describes in which files you can find the functions and what they are actually called.

For example, `core_course_get_contents' has the following:

'core_course_get_contents' => array(
    'classname' => 'core_course_external',
    'methodname' => 'get_course_contents',
    'classpath' => 'course/externallib.php',
    'description' => 'Get course contents',
    'type' => 'read',
    'capabilities' => 'moodle/course:update, moodle/course:viewhiddencourses',
    'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),

This means we have to search for the method 'get_course_contents' in the file 'course/externallib.php', which is located here. Just above it we also find get_course_contents_parameters, which explains the parameters of the function.

Testing the API functions

The Moodle web app (Working in Chromium) can be used to test the API functions. You can also quickly find out how it uses the functions.

Each request can be resent by copying the request to the webconsole as described here, then customizing it as desired and resending it.

Note that you must first disable CORS for content scripts, for that install and activate this extension. Warning, this is dangerous and you should only do this if you know what the consequences are.

An example fetch may look like this:

fetch("https://school.moodledemo.net/webservice/rest/server.php?moodlewsrestformat=json&wsfunction=mod_data_get_databases_by_courses", {
"headers": {
    "content-type": "application/x-www-form-urlencoded",
},
"body": "moodlewssettingfilter=true&moodlewssettingfileurl=true&wsfunction=mod_data_get_databases_by_courses&wstoken=a5f09233fecb324dce64e78fef749d45",
"method": "POST"
}).then(r => r.json()).then(json => console.log(json));