Create user using Moodle webservice

北战南征 提交于 2019-12-03 21:34:59

get same problem with required key 'users' solve problem with this =>

$serverurl = $domainname . '/webservice/rest/server.php'. '?wstoken=' . $token . '&wsfunction='.$functionname;
    //require_once('../curl.php');
    $curl = new curl;
    $params = "&users[0][username]=loginnn&users[0][password]=4815162342Qq*&users[0][firstname]=allala&users[0][lastname]=trest&users[0][email]=ty@mail.ru";
    //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->post($serverurl . $restformat, $params);

I think You must high the debugging level in your moodle system, I hope you will get more useful information regarding this error, debugging will help you to reach the exact problem. go through following path:

Home ► Site administration ► Development ► Debugging

choose Developer level from the debug messages and save the changes

I have a similar problem once, in my experience is a problema with $user1->password = 'testpassword1'; Moodle needs a password with one uppercase letter and at least one simbol like / . , - _ etc.

try a new password maybe it works...

Ravi Tiwari

Do not pass the ID into the user array, as it doesn't accept it as a parameter. For more details, please read the WebService API documentation for creating a user in Moodle.

Wang Peter

came across the same need today and I used your post to get the code from GitHUB so I guess I'd tell you how I fixed the error:

change your code to the following:

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

The code from GitHUB $user1 is an object. Moodle required an Array.

Below is copied from Moodle's documentation.

[users] =>
Array 
    (
    [0] =>
        Array 
            (
            [username] => string                
            [password] => string                
            [firstname] => string                
            [lastname] => string                
            [email] => string                
            [auth] => string                
            [idnumber] => string                
            [lang] => string                
            [calendartype] => string                
            [theme] => string                
            [timezone] => string                
            [mailformat] => int                
            [description] => string                
            [city] => string                
            [country] => string                
            [firstnamephonetic] => string                
            [lastnamephonetic] => string                
            [middlename] => string                
            [alternatename] => string                
            [preferences] =>
                Array 
                    (
                    [0] =>
                        Array 
                            (
                            [type] => string                                
                            [value] => string                                
                            )
                    )                
            [customfields] =>
                Array 
                    (
                    [0] =>
                        Array 
                            (
                            [type] => string                                
                            [value] => string                                
                            )
                    )                
            )
    )

In our case, we added Https support, but we were still calling the Http version of the Moodle url. Changing to Https solved the problem.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!