Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Creating a web service client: Difference between revisions

From MoodleDocs
Line 19: Line 19:


<code php>
<code php>
$function = 'group_create_groups'; //see the web service documentation
$group = new stdClass();
$params = array('search' => $search, 'downloadable' => $downloadable,
$group->courseid = $course->id;
    'enrollable' => !$downloadable, 'options' => $options);
$group->name = 'tmpgroufortest123';
$serverurl = $huburl . "/local/hub/webservice/webservices.php";
$group->enrolmentkey = '';
require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
$group->description = 'Group 1';
$xmlrpcclient = new webservice_xmlrpc_client($serverurl, $token);
$group2 = new stdClass();
$group2->courseid = $course->id;
$group2->name = 'tmpgroufortest1233';
$group2->enrolmentkey = '';
$group2->description = 'Group 2';
$paramgroups = array($group, $group2);
$function = 'moodle_group_create_groups';
$params = array('groups' => $paramgroups);
$serverurl = $remotemoodleurl . "/webservice/xmlrpc/server.php" . '?wstoken=' . $token;
require_once 'Zend/XmlRpc/Client.php';
$xmlrpcclient = new Zend_XmlRpc_Client($serverurl);
try {
try {
    $result = $xmlrpcclient->call($function, $params);
        $createdgroups = $client->call($function, $params);
    $courses = $result['courses'];
    $coursetotal = $result['coursetotal'];
} catch (Exception $e) {
} catch (Exception $e) {
     $errormessage = $OUTPUT->notification(
     var_dump($e);
        get_string('errorcourselisting', 'block_community', $e->getMessage()));
}
}
</code>
</code>



Revision as of 08:00, 18 January 2011

Moodle 2.0


You need to have already setup a web service. You can get help from How to enable web services for ordinary users or How to enable web services for an external system . Then have a look to your security keys page. You'll find there your token and the web service documentation associated with this token.

Officially supported protocols

  • REST: the Moodle REST server uses POST for parameters and XML for returned values. This server is not RESTfull.
  • SOAP: the Moodle SOAP server is based on the Zend SOAP server (itself based on the PHP SOAP server). Zend publishes a Zend SOAP client. The current server implementation doesn't work with Java/.Net because we didn't generated a fully describe WSDL yet. If you are working on a Java/.Net client, follow or participate to the tracker issue MDL-20804
  • XML-RPC: the Moodle XML-RPC server is based on Zend XML-RPC server. Zend also publishes a Zend XML-RPC client.
  • AMF: the Moodle AMF server is based on the Zend AMF server. The test client can be found in Settings blocks > Site Administration > Development > Web service test client > AMF Test client.

PHP client examples

In all these client examples we will create two groups calling the web service function group_create_groups.

SOAP

The Moodle SOAP server extends Zend SOAP Server. It is recommended to have a look to the Zend SOAP client documentation.

XML-RPC

The Moodle XML-RPC server extends Zend XML-RPC Server. It is recommended to have a look to the Zend XML-RPC client documentation

$group = new stdClass(); $group->courseid = $course->id; $group->name = 'tmpgroufortest123'; $group->enrolmentkey = ; $group->description = 'Group 1'; $group2 = new stdClass(); $group2->courseid = $course->id; $group2->name = 'tmpgroufortest1233'; $group2->enrolmentkey = ; $group2->description = 'Group 2'; $paramgroups = array($group, $group2); $function = 'moodle_group_create_groups'; $params = array('groups' => $paramgroups); $serverurl = $remotemoodleurl . "/webservice/xmlrpc/server.php" . '?wstoken=' . $token; require_once 'Zend/XmlRpc/Client.php'; $xmlrpcclient = new Zend_XmlRpc_Client($serverurl); try {

       $createdgroups = $client->call($function, $params);

} catch (Exception $e) {

   var_dump($e);

}

REST