我是网络服务的初学者。我正在 Moodle 中编写网络服务。
我想为用户注册编写一项服务。在这种情况下,该用户将没有任何令牌或用户名+密码。
我们如何允许该用户调用core_user_createUsers
Web 服务进行注册?
对于其他服务,我正在使用此代码:
$domainname = 'http://mydomain/moodle';
require_once('./curl.php');
$curl = new curl;
$authurl = $domainname .'/login/token.php?username=admin&password=Admin123$&service=shcs_services';
$resp = $curl->get($authurl);
if($resp)
{
$resp = json_decode($resp);
$authToken = $resp->token;
}
/// PARAMETERS
$serverurl = $domainname . '/webservice/rest/server.php';
$params['wstoken'] = $authToken;
$params['wsfunction'] = 'core_user_createUsers';
$params['moodlewsrestformat'] = 'json';
$user1 = new stdClass();
$user1->username = 'testusername';
$user1->password = 'Testpassword1%';
$user1->firstname = 'testfirstname1';
$user1->lastname = 'testlastname1';
$user1->email = 'testemail1@moodle.com';
$user1->auth = 'manual';
$user1->idnumber = 'testidnumber1';
$user1->lang = 'en';
$user1->theme = 'standard';
$user1->timezone = '-12.5';
$user1->mailformat = 0;
$user1->description = 'Hello World!';
$user1->city = 'testcity1';
$user1->country = 'au';
$preferencename1 = 'preference1';
$preferencename2 = 'preference2';
$user1->preferences = array(
array('type' => $preferencename1, 'value' => 'preferencevalue1'),
array('type' => $preferencename2, 'value' => 'preferencevalue2'));
$users = array($user1);
$params['users'] = $users;
$resp = $curl->post($serverurl, $params);
echo $resp;
在上面的代码中,我想跳过下面的部分
$authurl = $domainname .'/login/token.php?username=admin&password=Admin123$&service=shcs_services';
$resp = $curl->get($authurl);
if($resp)
{
$resp = json_decode($resp);
$authToken = $resp->token;
}
这样我就不想将值传递$params['wstoken'] = $authToken;
给 web service 。
但它不允许我没有令牌。这是我得到的错误:
{"exception":"moodle_exception","errorcode":"invalidtoken","message":"Invalid token - token not found"}