通过web服务php在moodle中创建用户

问题描述 投票:0回答:1

我试图通过使用PHP在Moodle中通过Web服务创建用户,我使用的是ws函数:core_user_create_users。但它给了我一个错误。

{“exception”:“webservice_access_exception”,“errorcode”:“accessexception”,“message”:“访问控制异常”}

我正在使用moodle ver3.4。

如果Web服务功能有问题,您能告诉我如何为用户添加当前令牌的功能吗?如果它的其他问题请告诉我。

谢谢,

php moodle
1个回答
1
投票

我会就如何做到这一点给出一个完整的答案。首先,我们将首先为Web服务创建一个角色(这不是必需的,但我喜欢这样做以保持管理员的实例清洁)。但首先,您可以快速解决问题。将授权用户添加到您网站的管理员列表中,您可以使用生成的令牌自由调用任何功能。

I. Create a new role for web services

你可以去Site administration > Users > Permissions > Define roles做到这一点。您将找到一个添加新角色Add a new role的按钮。单击该按钮后,您将获得一个表单,您可以使用已定义(或默认)角色或原型来定义新角色。您还可以使用另一个Moodle实例中的导出文件创建角色。无论如何,我们将选择Continue来定义我们的自定义角色。现在,我们可以填写必要的信息,如(短名称,自定义全名,自定义描述)。在上下文部分,选择System从系统中的任何位置分配此角色。现在向下滚动到功能。在Filter搜索框中搜索您的功能(在我们的示例中创建用户)。您可以在创建Web服务并向其添加功能时查看所需的功能。每个功能都需要具有特定功能的用户才能执行。您可以通过转到Site administration > Plugins > Web services > External services从管理仪表板找到功能功能并创建新的外部服务并添加您的功能,您将在服务表的Required capabilities列中看到功能列表。只需在函数下拉列表中键入core_user_create_users即可。点击Add functions按钮,现在你可以看到Required capabilities。在这种情况下,它只是moodle/user:create。所以我们可以在我们的角色中使用它来检查我们想要的每个功能旁边的allow复选框。完成角色配置后,单击Create this role

II. Assign a user to the new role

现在我们可以将用户分配给新角色。

  1. Site administrator > Users > Permissions > Assign system roles
  2. 选择我们的新角色。
  3. 将用户添加到现有用户列表中。

III. Add user to authorized users

只有在创建外部服务时检查了Only authorized users选项时才需要这样做。

  1. Site administrator > Plugins > Web services > External services
  2. 单击Web服务上的Authorized users链接。
  3. 将用户添加到Authorized users列表中。

检查表单下的警告。如果您的用户没有某些功能,Moodle会在表单下发出警告信号。您可以通过向我们之前创建的角色添加缺少的功能来解决此问题。

IV. Create a token

现在我们可以转到令牌管理器并为我们的用户生成一个新令牌以用于我们的外部Web服务。

BS:不要忘记先启用Web服务。启用REST协议或您愿意使用的任何协议。


Update: How to call the function?

根据本回答的评论中要求的详细信息,我将提供一个如何调用这些Web服务函数的示例(在此示例中为create_users)。

在这个例子中,我们将使用Moodle核心团队提供的cURL类来使我们更轻松。您可以在其Web服务客户端示例here的存储库中找到此类。我将在此存储库的curl.php文件夹中使用PHP-REST文件。此存储库中的所有PHP文件夹都具有相同的curl.php文件。 client.php文件是唯一的区别。

$token = 'replace it with your web service token';
$domainname = 'http://your_moodle_domain.ltd';
$functionname = 'core_user_create_users';
$restformat = 'json';

$user = array(
    "username" => "username of your new user", // must be unique.
    "password" => "password must respect moodle policies",
    "firstname" => "first name",
    "lastname" => "last name",
    "email" => "[email protected]",
    "auth" => 'authentication method can be email, manual, registred ..',
    "customfields" => array ( // If you have custom fields in your system.
        array(
            "type" => "birthdate",
            "value" => strtotime("01/01/1990")
            ),
        array(
            "type" => "something_else",
            "value" => "0"
            )
        )

);

$users = array($user); // must be wrapped in an array because it's plural.

$param = array("users" => $users); // the paramater to send


$serverurl = $domainname . '/webservice/rest/server.php'. '?wstoken=' . 
             $token . '&wsfunction='.$functionname;

require_once('curl.php'); // You can put it in the top.
$curl = new curl;

$restformat = ($restformat == 'json')?'&moodlewsrestformat=' . 
               $restformat:'';

$resp = $curl->post($serverurl . $restformat, $param);
© www.soinside.com 2019 - 2024. All rights reserved.