使用 Moodle Web 服务创建用户

Gui*_*rme 3 php web-services moodle

我尝试通过 Web 服务 api 在 Moodle 上创建一个新用户。

我尝试使用在 github 上找到的示例和另一个 php 代码

在两者中我都收到相同的回复:

“单一结构中缺少必需的密钥:用户”

响应:

{
    "exception":"invalid_parameter_exception",
    "errorcode":"invalidparameter",
    "message":"Invalid parameter value detected",
    "debuginfo":"Missing required key in single structure: users"
}
Run Code Online (Sandbox Code Playgroud)

我尝试通过数组更改对象,但错误仍然存​​在。

我的代码:

$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 = 'testemail1@moodle.com';
$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);  
Run Code Online (Sandbox Code Playgroud)

我有一个有效的令牌,并且允许用户使用该core_user_create_users功能

Анд*_*кин 6

与所需的关键“用户”遇到同样的问题解决这个问题=>

$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]=your_password1#Q&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);
Run Code Online (Sandbox Code Playgroud)