If you have experimented with the Moodle Web Services then you will find there are pockets of information that can be pieced together to help understand how the API’s work.

Below is a list of links to pages and sites that I have found extremely useful when it comes to putting together an external web service.

Moodle web service links

The first most important page is on your Moodle server.

yourmoodleserver/admin/webservice/documentation.php

This will list all the web service functions that you have available and describe the schema for each service in different protocols.

Current web service functions from the Moodle documentation.

https://docs.moodle.org/dev/Web_service_API_functions

Using web services – setting up the service on Moodle

https://docs.moodle.org/37/en/Using_web_services

Creating a web service client

https://docs.moodle.org/dev/Creating_a_web_service_client

Getting started list of steps to setup web services in Moodle

https://support.ecreators.com.au/hc/en-us/articles/360000719416-Step-by-Step-Instructions-for-how-Setup-external-services-Web-Services-within-Moodle

Step by step setup

https://moodle.org/mod/forum/discuss.php?d=319039

Quick web service setup and test

https://benit.github.io/blog/2017/03/29/consumming-a-moodle-webservice/

Example: Save grades using PHP.

https://gist.github.com/hig3/a34e896d0ce44f872188

Download CURL if you are using PHP as your server side code from here.

Some code to get you started

If you just want some code to get you started, the below code is the core of what you will need.

Make sure you setup your Moodle service as recommended in the above links to allow web services and setup a user account with a token.

Enrol the user with the web service token into the course you are going to test with.

Remember that your account must have permission to use a web service.

I am assuming you are using PHP as the external application server to call the Moodle web services?

On a PHP server, download the CURL PHP file from above and add it to your web server to be included by the code below.

Create a PHP testwebservice.php page and add the following code.

Replace courseid_as_integer with your test course ID.  

$config = array(

    'moodletoken' =>  'xxxxxx',
    'moodledomainname' => 'https://yourmoodleserver'
);
$courserefmoodle =  courseid_as_integer;
$restformat = 'json';
$functionname = 'core_course_get_courses';
$mdl_params = array('options' => array('ids' => array($courserefmoodle)));
$serverurl = $config['moodledomainname'] . '/webservice/rest/server.php'. '?wstoken=' . $config['moodletoken'] . '&wsfunction='.$functionname;
require_once('curl.php');
$curl = new curl;
//if rest format == 'xml', then we do not add the param for backward compatibility with Moodle < 2.2
$restformat = ($restformat == 'json')?'&moodlewsrestformat=' . $restformat:'';
$resp = json_decode($curl->post($serverurl . $restformat, $mdl_params));
//print_r($resp);
if (!isset($resp) || is_object($resp) || !isset($resp[0])) {
    echo "<div class='warning'>MOODLE Course not found or web service user is not enroled in this course.</div>";
    exit;
}
$coursedata = $resp[0];
print_r($coursedata);

This will return the details about the course ID that you set as JSON data.


Need more information….. Join the NEW Moodle Administrators and Developers group

https://www.facebook.com/groups/1616484371816446/

Would you like a FREE copy of the Top 10 Admin Tasks​ that you ​must know​ for Moodle Administrators?
You will discover some simple things that you can do in Moodle that will make your life easier.