PHP-XML-RPC

建立使用者

<?php
/**
 * XML-RPC client for MoodleSET
 *
 * @package    moodleset
 * @author     Jack Liou
 * @author     Cheng Wei
 * @copyright  2017 Click-AP {@link http://www.click-ap.com)
 */

// 請修改下列變數
$token = '1a2222bbcccd33333333e5f6g7777h88';
$moodlesethost = 'http://localhost/mod314';
$functionname = 'moodleset_create_users';

// 準備參數
$user1 = new stdClass();
$user1->username = 'clickap1';
$user1->password = 'Testpassword1!';
$user1->firstname = 'moodleset1';
$user1->lastname = 'ClickAP';
$user1->email = 'clickap1@click-ap.com';
$user1->auth = 'manual';
$user1->idnumber = 'moodleset1';
$user1->lang = 'en';
$user1->description = 'Hello Moodleset!';
$params = array($user1);

// 呼叫
header('Content-Type: text/plain');
$serverurl = $moodlesethost . '/webservice/xmlrpc/server.php'. '?wstoken=' . $token;
require_once('./curl.php');
$curl = new curl;
$request = xmlrpc_encode_request($functionname, array($params));
$response = xmlrpc_decode($curl->post($serverurl, $request));
print_r($response);

結果如下:
Array
(
    [0] => Array
        (
            [id] => 25
            [username] => moodleset1
        )

)  

建立類別

<?php
/**
 * XML-RPC client for MoodleSET
 *
 * @package    moodleset
 * @author     Jack Liou
 * @author     Cheng Wei
 * @copyright  2017 Click-AP {@link http://www.click-ap.com)
 */

// 請修改下列變數
$token = '1a2222bbcccd33333333e5f6g7777h88';
$moodlesethost = 'http://localhost/mod314';
$functionname = 'moodleset_create_categories';

// 準備參數
$category1 = new stdClass();
$category1->name = 'Clickap-01';
$category1->parent = 0;
$params = array($category1);

// 呼叫
header('Content-Type: text/plain');
$serverurl = $moodlesethost . '/webservice/xmlrpc/server.php'. '?wstoken=' . $token;
require_once('./curl.php');
$curl = new curl;
$request = xmlrpc_encode_request($functionname, array($params));
$response = xmlrpc_decode($curl->post($serverurl, $request));
print_r($response);

結果如下:
Array
(
    [0] => Array
        (
            [id] => 4
            [name] => testname2
        )

)

建立課程

<?php
/**
 * XML-RPC client for MoodleSET
 *
 * @package    moodleset
 * @author     Jack Liou
 * @author     Cheng Wei
 * @copyright  2017 Click-AP {@link http://www.click-ap.com)
 */

// 請修改下列變數
$token = '1a2222bbcccd33333333e5f6g7777h88';
$moodlesethost = 'http://localhost/mod314';
$functionname = 'moodleset_create_courses';

// 準備參數
$cou1 = new stdClass();
$cou1->fullname = 'ClickAP'; //課程全名
$cou1->shortname = 'Click';  //課程短名
$cou1->categoryid = 39; //類別ID
$params = array($cou1);

// 呼叫
header('Content-Type: text/plain');
$serverurl = $moodlesethost . '/webservice/xmlrpc/server.php'. '?wstoken=' . $token;
require_once('./curl.php');
$curl = new curl;
$request = xmlrpc_encode_request($functionname, array($params));
$response = xmlrpc_decode($curl->post($serverurl, $request));
print_r($response);

結果如下:
Array
(
    [0] => Array
        (
            [id] => 8
            [shortname] => Click
        )

)

加選

<?php
/**
 * XML-RPC client for MoodleSET
 *
 * @package    moodleset
 * @author     Jack Liou
 * @author     Cheng Wei
 * @copyright  2017 Click-AP {@link http://www.click-ap.com)
 */

// 請修改下列變數
$token = '1a2222bbcccd33333333e5f6g7777h88';
$moodlesethost = 'http://localhost/mod314';
$functionname = 'moodleset_enrol_users';

// 準備參數
$enroluser1 = new stdClass();
$enroluser1-> roleid = 8; //指定用戶角色的ID 
$enroluser1-> userid = 164; //要加選的用戶ID
$enroluser1-> courseid = 25; //要加選的課程ID
$params = array($enroluser1);

// 呼叫
header('Content-Type: text/plain');
$serverurl = $moodlesethost . '/webservice/xmlrpc/server.php'. '?wstoken=' . $token;
require_once('./curl.php');
$curl = new curl;
$request = xmlrpc_encode_request($functionname, array($params));
$response = xmlrpc_decode($curl->post($serverurl, $request));
print_r($response);


ċ
curl.php
(19k)
wei.chi@click-ap.com,
2017年3月9日 下午7:14
Comments