PHP-REST

REST是什麼?
REST是英文Representational State Transfer的縮寫,中文翻譯:表述性狀態轉移。

MoodleSET安裝後, 提供多種功能的 RESTful API,說明如下 :

建立使用者

<?php
/**
 * REST client for MoodleSET 3.1
 *
 * @author Jack{@link jack@click-ap.com}
 * @author bin {@link bin.chen@click-ap.com}
 * @copyright Click-AP {@http://www.click-ap.com}
 */
// 請修改下列變數
$token = '1a2222bbcccd33333333e5f6g7777h88';
$moodleset = 'http://localhost/mod314';
$functionname = 'moodleset_create_users';
$user1 = new stdClass();
$user1->username = 'moodlesetuser001';
$user1->password = 'Testpassword1!';
$user1->firstname = 'ModSET1';
$user1->lastname = 'ClickAP';
$user1->email = 'modset001@click-ap.com';
$user1->auth = 'manual';
$user1->idnumber = 'testidnumber1';
$user1->lang = 'en';
$user1->description = 'Hello MoodleSET!';
$user2 = new stdClass();
$user2->username = 'moodlesetuser002';
$user2->password = 'Testpassword2!';
$user2->firstname = 'ModSET2';
$user2->lastname = 'ClickAP';
$user2->email = 'modset002@click-ap.com';
$params = array($user1, $user2);

// 呼叫 MoodleSET RESTful 
header('Content-Type: text/plain');
$serverurl = $moodleset . '/webservice/rest/server.php'. '?wstoken=' . $token . '&wsfunction='.$functionname;
require_once('./curl.php');
$curl = new curl;
$restformat = '&moodlewsrestformat=json';
$resp = $curl->post($serverurl . $restformat, array('users'=>$params));
print_r($resp);

結果如下:
[{"id":27,"username":"moodlesetuser001"},{"id":28,"username":"moodlesetuser002"}]

建立類別

<?php
/**
 * REST client for MoodleSET 3.1
 *
 * @author Jack,bin {@link jack@click-ap.com}
 @author bin {@link bin.chen@click-ap.com}
 * @copyright Click-AP {@http://www.click-ap.com}
 */

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

$cat1 = new stdClass();
$cat1->name = 'moodlesetCategories1';
$cat1->parent = 0;
$params = array($cat1);

// 呼叫 MoodleSET RESTful
header('Content-Type: text/plain');
$serverurl = $moodleset . '/webservice/rest/server.php'. '?wstoken=' . $token . '&wsfunction='.$functionname;
require_once('./curl.php');
$curl = new curl;
$restformat = '&moodlewsrestformat=json';
$resp = $curl->post($serverurl . $restformat, array('categories'=>$params));
print_r($resp);

結果如下:
[{"id":5,"name":"moodlesetCategories1"}]

建立課程

<?php
/**
 * REST client for MoodleSET 3.1
 *
 * @author Jack {@link jack@click-ap.com}
 @author bin {@link bin.chen@click-ap.com} 
 * @copyright Click-AP {@http://www.click-ap.com}
 */

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

$cat1 = new stdClass();
$cat1->fullname = 'MathCourses';
$cat1->shortname = 'Math';
$cat1->categoryid = 5;
$params = array($cat1);

// 呼叫 MoodleSET RESTful
header('Content-Type: text/plain');
$serverurl = $moodlesethost . '/webservice/rest/server.php'. '?wstoken=' . $token . '&wsfunction='.$functionname;
require_once('./curl.php');
$curl = new curl;
$restformat = '&moodlewsrestformat=json';
$resp = $curl->post($serverurl . $restformat, array('courses'=>$params));
print_r($resp);

結果如下:
[{"id":9,"shortname":"Math"}]

加選

<?php
/**
 * REST client for MoodleSET 3.1
 *
 * @author Jack {@link jack@click-ap.com}
 @author bin {@link bin.chen@click-ap.com}
 * @copyright Click-AP {@http://www.click-ap.com}
 */

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

$cat1 = new stdClass();
$cat1->roleid = 8;  
$cat1->userid  = 11;
$cat1->courseid = 2;
$params = array($cat1);

// 呼叫 MoodleSET RESTful
header('Content-Type: text/plain');
$serverurl = $moodleset . '/webservice/rest/server.php'. '?wstoken=' . $token . '&wsfunction='.$functionname;
require_once('./curl.php');
$curl = new curl;
$restformat = '&moodlewsrestformat=json';
$resp = $curl->post($serverurl . $restformat, array('enrolments'=>$params));
print_r($resp);

結果如下:
null


Comments