我试图通过网络服务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
功能
答案 0 :(得分:4)
与所需密钥'用户'相同的问题 用这个=>
解决问题$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);
答案 1 :(得分:1)
我认为您必须提高moodle系统的调试级别,我希望您能获得有关此错误的更多有用信息,调试将帮助您解决确切的问题。通过以下路径:
Home ► Site administration ► Development ► Debugging
从Developer level
中选择debug messages
并保存更改
答案 2 :(得分:1)
我有一个类似的问题,根据我的经验,问题是@Component({
selector: 'panel-product-selection',
template,
providers: [ProductTreeService, UserApi],
directives: [ProductTreeComponent]
})
export class PanelProductSelectionComponent
{
private categoriesProductsTree: Collections.LinkedList<CategoryTreeElement>;
private productTreeService: ProductTreeService;
private userApi: UserApi;
constructor(productTreeService: ProductTreeService, userApi: UserApi)
{
this.productTreeService = productTreeService;
this.userApi = userApi;
}
ngOnInit(): void
{
//...
}
}
Moodle需要一个大写字母的密码和至少一个像/的simbol。 , - _等
尝试新密码也许有效......
答案 3 :(得分:0)
不要将ID传递给用户数组,因为它不接受它作为参数。有关更多详细信息,请阅读WebService API文档以在Moodle中创建用户。
答案 4 :(得分:0)
今天遇到了同样的需求,我用你的帖子从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
)
)
)
)
答案 5 :(得分:0)
在我们的案例中,我们添加了Https支持,但我们仍在调用Moodle url的Http版本。改为Https解决了这个问题。
答案 6 :(得分:0)
这已经过测试并且可以在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);