php – 使用Moodle webservice创建用户
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 使用Moodle webservice创建用户,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2115字,纯文字阅读大概需要4分钟。
内容图文
![php – 使用Moodle webservice创建用户](/upload/InfoBanner/zyjiaocheng/733/7a0e2ef13fa6462dbbda06a1497c4ed6.jpg)
我试图通过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函数
解决方法:
与所需的密钥’用户’相同的问题
用这个解决问题=>
$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);
内容总结
以上是互联网集市为您收集整理的php – 使用Moodle webservice创建用户全部内容,希望文章能够帮你解决php – 使用Moodle webservice创建用户所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。