3
I have been trying to add a new user to Moodle using your webservice api
I have already modified several times the parameters passed via post, tested using an array instead of an object, tested some changes in the structure of the array that is passed, but always get the same response from Moodle:
"Missing required key in single Structure: users"
The answer:
{
"exception":"invalid_parameter_exception",
"errorcode":"invalidparameter",
"message":"Valor inv\u00e1lido de par\u00e2metro detectado",
"debuginfo":"Missing required key in single structure: users"
}
My code:
$functionname = 'core_user_create_users';
$user1 = new stdClass();
$user1->id = 1; //
$user1->username = 'testusername1';
$user1->password = 'testpassword1';
$user1->firstname = 'testfirstname1';
$user1->lastname = 'testlastname1';
$user1->email = '[email protected]';
$user1->auth = 'manual';
$user1->idnumber = 'testidnumber1';
$user1->description = 'Hello World!';
$user1->city = 'testcity1';
$user1->country = 'BR';
$token = 'mytoken';
$domainname = 'localhost/moodle';
$functionname = 'core_user_create_users';
$restformat = 'json';
$serverurl = $domainname . '/webservice/rest/server.php'. '?wstoken=' . $token . '&wsfunction='.$functionname.'&moodlewsrestformat=' . $restformat;
$users = array($user1);
$params = array('users' => $users);
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => 'Content-Type: text/plain',
'content' => $params
)
));
$contents = file_get_contents($serverurl, null, $context);
//print_r($contents);
$resposta = json_decode($contents);
From what I’ve checked, all mandatory fields are filled in.
I am using a valid token, and the user has the necessary permission to use the function core_user_create_users
.
I used the following code as groundwork
UPDATE
Among the common mistakes when trying to create a user via webservice are
Try to register required field with empty value
Try to register password that does not meet the password policy defined in the Moodle configuration
Forget to encapsulate the object(s) (s) with the user(s) information in an array and switch to 'users''
$users = array($user1, $user2); $params = array('users' => $users);
I had performed tests with other passwords,
$user1->password = 'Password123!';
But I’ll check the security settings though.– Guilherme
Just for enrichment: I found this topic in which the problem was very similar. The striking solution was the same as the one suggested by the friend.
– Edgar Muniz Berlinck
I even disabled the password policy. But nothing has changed.
– Guilherme
@Edgarmunizberlinck I had already bumped into this topic previously too...
– Guilherme
I couldn’t remember exactly how I solved the situation, so I updated the question by mentioning the most common errors I found in the tests I did with the Moodle webservice
– Guilherme