PHP code example of warnax-corp / moodlerest

1. Go to this page and download the library: Download warnax-corp/moodlerest library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

warnax-corp / moodlerest example snippets






$MoodleRest = new MoodleRest('http://127.0.0.1/moodle/webservice/rest/server.php', '8f12e614dae30735260a045313caa400');

$groups = $MoodleRest->request('core_group_get_groups', array('groupids' => array(1,2)));

print_r($groups);

$MoodleRest = new MoodleRest('http://127.0.0.1/moodle/webservice/rest/server.php', '8f12e614dae30735260a045313caa400');

$new_group = array('groups' => array(array('courseid' => 2, 'name' => 'Group name', 'description' => 'Group description')));

// The default request's METHOD is to make a GET request, but you can change it to POST. This is recommended when inserting and updating data.
$return = $MoodleRest->request('core_group_create_groups', $new_group, MoodleRest::METHOD_POST);

// If you want the requested URL
echo $MoodleRest->getUrl();

$MoodleRest = new MoodleRest();
$MoodleRest->setServerAddress("http://127.0.0.1/moodle/webservice/rest/server.php");
$MoodleRest->setToken('8f12e614dae30735260a045313caa400');
$MoodleRest->setReturnFormat(MoodleRest::RETURN_ARRAY); // Array is default. You can use RETURN_JSON or RETURN_XML too.

// You can enable debugging information using setDebug()
// When debugging is enabled, after each request the built URL and the result returned by the webservice function are printed to the standard output.
$MoodleRest->setDebug();
$arr = $MoodleRest->request('core_group_get_groups', array('groupids' => array(1,2)), MoodleRest::METHOD_GET);

// Note: You can make more requests using the same object

/*
The $parameters variable below translates in URL as:
    userlist[0][userid]=5&
    userlist[0][courseid]=2&
    userlist[1][userid]=4&
    userlist[1][courseid]=2"
*/
$parameters = array('userlist' => array(array('userid' => 5, 'courseid' => 2), array('userid' => 4, 'courseid' => 2)));

$json =
    (new MoodleRest())->setServerAddress("http://127.0.0.1/moodle/webservice/rest/server.php")->
    setToken('8f12e614dae30735260a045313caa400')->
    setReturnFormat(MoodleRest::RETURN_JSON)->request('core_user_get_course_user_profiles', $parameters);

echo $json;