PHP通过Webservice在moodle中创建用户

标签 php web-services moodle moodle-api

我想将外部网站与 ​​moodle 系统连接起来。我已经设置了 webService 并创建了一个 token 来获取访问权限。

我关注了http://www.rumours.co.nz/manuals/using_moodle_web_services.htm设置但相比之下,我想通过 REST 实现连接,如 https://github.com/moodlehq/sample-ws-clients/find/master

我的方法是使用一个 moodle 类来处理数据交换。首先,我只是想尝试通过 webService 创建一些新的硬编码用户,但是 Moodle-Response 失败了:

“invalidrecord 在数据库表 external_functions 中找不到数据记录。”

在我看来好像调用成功了,但是 moodle 在找到“core_user_create_users”函数时遇到了问题。我检查了本地 moodle 数据库,在表 external_functions 中有一个“core_user_create_users”的条目,所以我有点困惑,moodle 不知道该怎么做。

那是我的课:

require_once (DOCUMENT_ROOT.'/tcm/api/moodle/curl.php');

class Moodle {

    private $token;          
    private $domainName;     // 'local.moodle.dev';
    private $serverUrl;

    public function __construct($token, $domainName) {
        $this->token = $token;
        $this->domainName = $domainName;
        $this->serverUrl = $this->domainName . '/webservice/rest/server.php' . '?wstoken=' . $this->token;

        echo "initialize Service: $this->serverUrl </br>";
    }


    public function createUser() {
        $functionName = 'core_user_create_users';

        /// PARAMETERS - NEED TO BE CHANGED IF YOU CALL A DIFFERENT FUNCTION
        $user1 = new stdClass();
        $user1->username = 'testusername1';
        $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'));
        $user2 = new stdClass();
        $user2->username = 'testusername2';
        $user2->password = 'testpassword2';
        $user2->firstname = 'testfirstname2';
        $user2->lastname = 'testlastname2';
        $user2->email = 'testemail2@moodle.com';
        $user2->timezone = 'Pacific/Port_Moresby';
        $users = array($user1, $user2);
        $params = array('users' => $users);

        /// REST CALL
        $serverurl = $this->serverUrl .  '&wsfunction=' . $functionName;
        require_once (DOCUMENT_ROOT.'/tcm/api/moodle/curl.php');
        $curl = new curl;
        //if rest format == 'xml', then we do not add the param for   backward compatibility with Moodle < 2.2
        $restformat = "json";
        $resp = $curl->post($serverurl . $restformat, $params);
        //print_r($resp);

        echo '</br>*************Server Response*************</br>';
        var_dump($resp);
   }

}

我正在使用我在上面发布的同一个 github 项目中的 curl 类——moodle 在他们的文档中链接到它。 docs.moodle.org/dev/Creating_a_web_service_client

我调用的入口点现在是硬编码的:

<?php

include_once (DOCUMENT_ROOT.'/tcm/api/moodle/moodle.php');
//entry point of code

if (isset($_POST)){
    //token and domain would be in $_POST
    $bla = new Moodle('0b5a1e98061c5f7fb70fc3b42af6bfc4', 'local.moodle.dev');
    $bla->createUser();
}

有谁知道如何解决“invalidrecord Can not find data record in database table external_functions”错误或有不同的方法/建议如何远程创建我的用户?

提前致谢

最佳答案

我终于用下面的代码得到了它:

class Moodle {

    private $token;          //'0b5a1e98061c5f7fb70fc3b42af6bfc4';
    private $domainName;     // 'http://local.moodle.dev';
    private $serverUrl;
    public $error;

    public function __construct($token, $domainName) {
        $this->token = $token;
        $this->domainName = $domainName;

        $this->serverUrl = $this->domainName . '/webservice/rest/server.php' . '?wstoken=' . $this->token;

        echo "initialize Service: $this->serverUrl </br>";
    }

    public function createUser() {
        $functionName = 'core_user_create_users';

        $user1 = new stdClass();
        $user1->username = 'testusername1';
        $user1->password = 'Uk3@0d5w';
        $user1->firstname = 'testfirstname1';
        $user1->lastname = 'testlastname1';
        $user1->email = 'testemail1@moodle.com';
        $user1->auth = 'manual';
        $user1->idnumber = '';
        $user1->lang = 'en';
        $user1->timezone = 'Australia/Sydney';
        $user1->mailformat = 0;
        $user1->description = '';
        $user1->city = '';
        $user1->country = 'AU';     //list of abrevations is in yourmoodle/lang/en/countries
        $preferencename1 = 'auth_forcepasswordchange';
        $user1->preferences = array(
            array('type' => $preferencename1, 'value' => 'true')
            );

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

        /// REST CALL
        $restformat = "json";
        $serverurl = $this->serverUrl . '&wsfunction=' . $functionName. '&moodlewsrestformat=' . $restformat;
        require_once (DOCUMENT_ROOT . '/tcm/api/moodle/curl.php');
        $curl = new curl();


        $resp = $curl->post($serverurl, $params);


        echo '</br>************************** Server Response    createUser()**************************</br></br>';
        echo $serverurl . '</br></br>';

        var_dump($resp);
    }
}

信息:

对于所有 moodle 初学者.. 激活 moodle 调试消息有点帮助。您会在服务器返回的响应中收到额外的错误信息。

Moodle -> 站点管理 -> 开发 -> 调试 -> 调试信息

选择:DEVELOPER:extra Moodle debug messages for developers

关于PHP通过Webservice在moodle中创建用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33404353/

相关文章:

javascript - CakePhp 将变量转换为 JS 变量

php - 每天、每周和每月的总和

php - 如何使用 Postgre 安装 Moodle

mysql - 显示表格中的随机问题,不重复

java - Java Jersey 和 Microsoft .NET 之间的 Web 服务

php - Excel 中的用户导出在 Moodle 中损坏

php - mongodb-php 更新 $pull

php - 点击提交按钮后无法从数组中的数据库回显一个值(id)

iphone - 应用程序和 Web 浏览器的公共(public) session

Java 创建的 Soap 客户端没有得到响应,但 SOAPUI 也能正常工作