php 使用Moodle webservice创建用户

rta7y2nd  于 2023-04-04  发布在  PHP
关注(0)|答案(8)|浏览(188)

我尝试通过Web服务API在Moodle上创建一个新用户。
我用在github上找到的example和另一个php代码进行了尝试
在这两个我收到相同的响应:

“在单个结构中缺少必需的键:用户”

答案是:

{
    "exception":"invalid_parameter_exception",
    "errorcode":"invalidparameter",
    "message":"Invalid parameter value detected",
    "debuginfo":"Missing required key in single structure: users"
}

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

$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);

我有一个有效的令牌,允许用户使用core_user_create_users函数

mxg2im7a

mxg2im7a1#

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);
wtzytmuj

wtzytmuj2#

我认为你必须提高你的moodle系统的调试水平,我希望你能得到更多关于这个错误的有用信息,调试将帮助你找到确切的问题。
Home ► Site administration ► Development ► Debugging
debug messages中选择Developer level并保存更改

rhfm7lfc

rhfm7lfc3#

我有一个类似的问题,在我的经验是一个problema与$user1->password = 'testpassword1'; Moodle需要一个密码与一个大写字母和至少一个simbol像/ .,- _等。
尝试一个新的密码也许它工作...

lxkprmvk

lxkprmvk4#

不要将ID传递给用户数组,因为它不接受ID作为参数。更多细节,请阅读在Moodle中创建用户的WebService API文档。

7gcisfzg

7gcisfzg5#

我今天遇到了同样的需求,我用你的帖子从GitHUB获取代码,所以我想我会告诉你我是如何修复错误的:
将您的代码更改为以下内容:

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

GitHUB $user1中的代码是一个对象。Moodle需要一个数组。
下面是从Moodle的文档中复制的。

[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                                
                            )
                    )                
            )
    )
fgw7neuy

fgw7neuy6#

在我们的例子中,我们添加了对Http的支持,但是我们仍然调用Moodle url的Http版本。

hivapdat

hivapdat7#

这是测试和工作在Moodle 3.1.2 -我发现这是一个更干净,更容易理解的解决方案,使用适当的数组(如Moodle期望)本机cURL和http_build_query...
确保您提供给Moodle的密码符合您的安全要求。

$moodledata['users'][] = array(
        'username' => 'testuser123', 
        'password' => 'TestPass1!',
        'firstname' => 'Firstname',
        'lastname' => 'Lastname',
        'email' => 'test@test.com',
    );
    
    //open connection
    $token = 'your-token-goes-here';
    $domainname = 'https://yourmoodlesite.tld';
    $functionname = 'core_user_create_users';
    
    $url = $domainname . '/webservice/rest/server.php?wstoken=' . $token . '&wsfunction='.$functionname."&moodlewsrestformat=json";
    $ch = curl_init();

    //set the url, number of POST vars, POST data
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($moodledata));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    //execute post, and get our JSON as defined above with moodlewsrestformat=json
    $result = curl_exec($ch);
        
    //if you want it in an array
    //$result = json_decode($result, true);
xu3bshqb

xu3bshqb8#

我做了同样的代码,但我得到异常作为{“exception”:“invalid_parameter_exception”,“errorcode”:“invalidparameter”,“message”:“Invalid parameter value detected”,“debuginfo”:“users =〉Invalid parameter value detected:在单个结构中缺少所需的键:username”}。可能是什么错误。

moodledata['users'][] = array(
    'username' => 'testuser123', 
    'password' => 'TestPass1!',
    'firstname' => 'Firstname',
    'lastname' => 'Lastname',
    'email' => 'test@test.com',
);

//open connection
$token = 'your-token-goes-here';
$domainname = 'https://yourmoodlesite.tld';
$functionname = 'core_user_create_users';

$url = $domainname . '/webservice/rest/server.php?wstoken=' . $token . '&wsfunction='.$functionname."&moodlewsrestformat=json";
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($moodledata));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//execute post, and get our JSON as defined above with moodlewsrestformat=json
$result = curl_exec($ch);
    
//if you want it in an array
//$result = json_decode($result, true);

相关问题