Consuming Moodle Web Service

4

I'm trying to consume the moodle webservice, following a js / rest example that I found in github, I created a test.php with the code below (changing the domainname and token), but when I call the page I do not receive anything in the variable response , by chrome I get a Failed to load resource: net :: ERR_TIMED_OUT inside server.php.

My idea is to consume another webservice function, but not even the simple example I'm getting, Moodle already has the functions of webservice / rest activated and everything.

Something I'd like to add is that I can use the service through the url (below an example of another webservice function):

/moodle/webservice/rest/server.php?wstoken=meutoken&wsfunction=core_user_get_users&moodlewsrestformat=json&criteria[0][key]=departament&criteria[0][value]=1

So it returns me a json, however I needed to use $ .ajax to be able to manipulate this data, in case it shows the name of some user using an email or other parameter.

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.js"></script><scripttype="text/javascript">
$(document).ready(function() {
    var domainname = 'http://yourmoodle';
    var token = 'acabec9d208978d986986g987657ffg9';
    var functionname = 'core_user_create_users';
    var serverurl = domainname + '/webservice/rest/server.php' ;
    //add params into data
    var userstocreate = [{  username: 'testusername1',
                            password: 'testpassword1',
                            firstname: 'testfirstname1',
                            lastname: 'testlastname1',
                            email: '[email protected]',
                            auth: 'manual',
                            idnumber: 'testidnumber1',
                            lang: 'en',
                            theme: 'standard',
                            timezone: '-12.5',
                            mailformat: 0,
                            description: 'Hello World!',
                            city: 'testcity1',
                            country: 'au',
                            preferences: [
                                {type: 'preference1', value: 'preferencevalue1'},
                                {type: 'preference2', value: 'preferencevalue2'}
                            ]
                         },
                         {  username: 'testusername2',
                            password : 'testpassword2',
                            firstname : 'testfirstname2',
                            lastname : 'testlastname2',
                            email : '[email protected]',
                            timezone : 'Pacific/Port_Moresby'
                         }
                     ];
    var data = {
                wstoken: token,
                wsfunction: functionname,
                moodlewsrestformat: 'json',
                users: userstocreate
                }
    var response = $.ajax(
                            {   type: 'POST',
                                data: data,
                                url: serverurl
                            }
                         );
    console.info(response);
});
</script>
</head>
<body>
    Check your Javascript console for the "responseText" value.
</body>
</html>

server.php (Moodle Standard)

<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.


/**
 * REST web service entry point. The authentication is done via tokens.
 *
 * @package    webservice_rest
 * @copyright  2009 Jerome Mouneyrac
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

/**
 * NO_DEBUG_DISPLAY - disable moodle specific debug messages and any errors in output
 */
define('NO_DEBUG_DISPLAY', true);

/**
 * NO_MOODLE_COOKIES - no cookies with web service
 */
define('NO_MOODLE_COOKIES', true);

require('../../config.php');
require_once("$CFG->dirroot/webservice/rest/locallib.php");

if (!webservice_protocol_is_enabled('rest')) {
    debugging('The server died because the web services or the REST protocol are not enable',
        DEBUG_DEVELOPER);
    die;
}

$server = new webservice_rest_server(WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN);
$server->run();
die;
    
asked by anonymous 14.05.2015 / 18:45

2 answers

2

I could not understand why the code in js does not work but I was able to consume using an example in php that is in the same repository of the javascript example.

<?php



$token = 'meutoken';
$domainname = 'http://meumoodle';
$functionname = 'core_user_get_users';


$restformat = 'json'; //Also possible in Moodle 2.2 and later: 'json'
                     //Setting it to 'json' will fail all calls on earlier Moodle version
//////// moodle_user_create_users ////////
/// PARAMETERS - NEED TO BE CHANGED IF YOU CALL A DIFFERENT FUNCTION
$user1 = new stdClass();
$user1->key = 'username';
$user1->value = 'myvalue';

$users = array($user1);
$params = array('criteria' => $users);



$serverurl = $domainname . '/webservice/rest/server.php'. '?wstoken=' . $token . '&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 = $curl->get($serverurl . $restformat, $params);
$json = json_encode($resp);

$obj = json_decode($resp);


?>

Then just print out the $ obj value. In this example I put in response I am using another function "core_user_get_users", but the base is the same, it is only necessary to change the parameters according to the Moodle API documentation.

    
20.05.2015 / 19:33
0

I was having the same problem as you, but I managed to solve by looking at your code.

Moodle has some conditions for inserting data in the pack that changes according to the version of the platform.

For example, in the version I'm using the password must have at least one uppercase character and one alphanumeric character.

That was the error message that came back when I used part of your code. Then just change the password for the conditions requested to work.

    
15.09.2015 / 15:54