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;