PHP-SOAP


WSDL網址:
e.g.http://moodleset.clickap.com/moodleset/webservice/soap/server.phpwsdl=1
&wstoken=1a2222bbcccd33333333e5f6g7777h887

建立課程類別


<?php
/**
 * SOAP client for MoodleSET
 *
 * @package    moodleset
 * @author     Jack Liou
 * @copyright  2017 Click-AP {@http://www.click-ap.com}
 */

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

// PARAMETERS - NEED TO BE CHANGED IF YOU CALL A DIFFERENT FUNCTION
$category1 = new stdClass();
$category1->name = 'category1';
$category1->parent = 0;
$params = array($category1);

// Call SOAP 
$serverurl = $moodlesethost . '/webservice/soap/server.php'. '?wsdl=1&wstoken=' . $token;

$xml = simplexml_load_file($serverurl);
$faulcode = $xml->xpath('/SOAP-ENV:Envelope/SOAP-ENV:Body/SOAP-ENV:Fault/faultcode');
if (!empty($faulcode[0])) {
    $faultcode = (array) $faulcode[0];
    print_r($faultcode[0]);

    $faultstring = $xml->xpath('/SOAP-ENV:Envelope/SOAP-ENV:Body/SOAP-ENV:Fault/faultstring');
    if (!empty($faultstring[0])) {
        $faultstring = (array) $faultstring[0];
        print_r('<BR/>');
        print_r($faultstring[0]);
    }
    die();
}

// Call MoodleSET soap
$client = new SoapClient($serverurl);
try {
    $response = $client->__soapCall($functionname, array($params));
} catch (Exception $e) {
    print_r($e);
}
if (isset($response)) {
    print_r($response);
}

結果如下:

Array ( [0] => Array ( [id] => 6 [name] => category1 ) )


建立使用者


<?php
/**
 * SOAP client for MoodleSET
 *
 * @package    moodleset
 * @author     Jack Liou
 * @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 = 'testusername1';
$user1->password = 'Testpassword1!';
$user1->firstname = 'testfirstname1';
$user1->lastname = 'testlastname1';
$user1->email = 'testemail1@click-ap.com';
$user1->auth = 'manual';
$user1->idnumber = 'testidnumber1';
$user1->lang = 'en';
$user1->description = 'Hello World!';

$user2 = new stdClass();
$user2->username = 'testusername2';
$user2->password = 'Testpassword2!';
$user2->firstname = 'testfirstname2';
$user2->lastname = 'testlastname2';
$user2->email = 'testemail2@click-ap.com';
$params = array($user1, $user2);

// Call 
$serverurl = $moodlesethost . '/webservice/soap/server.php'. '?wsdl=1&wstoken=' . $token;

$xml = simplexml_load_file($serverurl);
$faulcode = $xml->xpath('/SOAP-ENV:Envelope/SOAP-ENV:Body/SOAP-ENV:Fault/faultcode');
if (!empty($faulcode[0])) {
    $faultcode = (array) $faulcode[0];
    print_r($faultcode[0]);

    $faultstring = $xml->xpath('/SOAP-ENV:Envelope/SOAP-ENV:Body/SOAP-ENV:Fault/faultstring');
    if (!empty($faultstring[0])) {
        $faultstring = (array) $faultstring[0];
        print_r('<BR/>');
        print_r($faultstring[0]);
    }
    die();
}

// Call MoodleSET soap
$client = new SoapClient($serverurl);
try {
    $response = $client->__soapCall($functionname, array($params));
} catch (Exception $e) {
    print_r($e);
}
if (isset($response)) {
    print_r($response);
}

結果如下:
Comments