PHP通过Webservice在moodle中创建用户

时间:2015-10-29 01:15:02

标签: php web-services moodle moodle-api

我想用moodle系统连接一个外部网站。我已经设置了webService并创建了一个令牌来获取访问权限。

我已经设置了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-project中的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();
}

有谁知道如何解决&#34; invalidrecord无法在数据库表external_functions中找到数据记录&#34;错误或有不同的方法/建议如何我可以远程创建我的用户??

提前致谢

1 个答案:

答案 0 :(得分:1)

我终于使用了以下代码:

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 - &gt;网站管理 - &gt;发展 - &gt;调试 - &gt;调试消息

选择:DEVELOPER:面向开发人员的额外Moodle调试消息