Note: You are currently viewing documentation for Moodle 2.9. Up-to-date documentation for the latest stable version of Moodle may be available here: Creating a web service client.

Development:Creating a web service client: Difference between revisions

From MoodleDocs
No edit summary
 
(19 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Moodle_2.0}}
{{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 [https://docs.moodle.org/en/How_to_get_a_security_key security keys] page. You'll find there your token and the web service documentation associated with this token.
[[Image:Moodle web service function documentation.jpg|thumb]]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 [https://docs.moodle.org/en/How_to_get_a_security_key security keys] page. You'll find there your token and the web service documentation associated with this token.


= Officially supported protocols=
= Officially supported protocols=


* '''REST''': the current REST Moodle server use POST paramters and XML. This server is not RESTfull. A client would be really easy to implement once you know how the XML is structured. Have a look to the Moodle default test client, the XML is displayed.
* '''REST''': the Moodle REST server uses POST for parameters and XML for returned values. This server is not RESTfull.
* '''SOAP''': the current SOAP server is based on the Zend SOAP sever (itself based on the PHP SOAP sever). Zend publishes [http://framework.zend.com/manual/en/zend.soap.client.html a Zend SAOP client]. The current server implementation doesn't work with Java/.Net because we didn't generated [http://geekandpoke.typepad.com/geekandpoke/2009/11/service-calling-made-easy-part-1.html a fully describe WSDL] yet. If you are working on a Java/.Net client, follow or participate to the tracker issue MDL-20804
* '''SOAP''': the Moodle SOAP server is based on the Zend SOAP server (itself based on the PHP SOAP server). Zend publishes [http://framework.zend.com/manual/en/zend.soap.client.html a Zend SOAP client]. The current server implementation doesn't work with Java/.Net because we didn't generated [http://geekandpoke.typepad.com/geekandpoke/2009/11/service-calling-made-easy-part-1.html 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 current XML-RPC server is based on Zend XML-RPC server. Zend also publishes [http://framework.zend.com/manual/en/zend.xmlrpc.client.html a Zend XML-RPC client].
* '''XML-RPC''': the Moodle XML-RPC server is based on Zend XML-RPC server. Zend also publishes [http://framework.zend.com/manual/en/zend.xmlrpc.client.html a Zend XML-RPC client].
* '''AMF''': the server is based on the Zend AMF server. The server has been tested with simple web service function and primary type. The test client can be found in ''Settings blocks > Site Administration > Development > Web service test client > AMF Test 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 =
= PHP client examples =
In all these client examples we will create two groups calling the web service function ''moodle_group_create_groups''.


== SOAP ==
== XML-RPC ==
[http://framework.zend.com/manual/en/zend.soap.client.html Zend SOAP client]
The Moodle XML-RPC server extends Zend XML-RPC Server. It is recommended to have a look to the [http://framework.zend.com/manual/en/zend.xmlrpc.client.html Zend XML-RPC client documentation]


<code php>
<code php>
$function = 'the_function_name'; //see the web service documentation
 
    $params = array('search' => $search, 'downloadable' => $downloadable,
include 'Zend/Loader/Autoloader.php';
        'enrollable' => !$downloadable, 'options' => $options);
Zend_Loader_Autoloader::autoload('Zend_Loader');
    $serverurl = $huburl . "/local/hub/webservice/webservices.php";
 
    require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
$function = "moodle_user_get_users_by_id";
    $xmlrpcclient = new webservice_xmlrpc_client($serverurl, $token);
 
    try {
// id of the users
        $result = $xmlrpcclient->call($function, $params);
$params = array(array(2,4));
        $courses = $result['courses'];
 
        $coursetotal = $result['coursetotal'];
$serverurl = "http://{servername}/webservice/xmlrpc/server.php?wstoken=$token";
    } catch (Exception $e) {
 
        $errormessage = $OUTPUT->notification(
$client = new Zend_XmlRpc_Client($serverurl);
                        get_string('errorcourselisting', 'block_community', $e->getMessage()));
print "<pre>";
    }
try {
    $data = $client->call($function, $params);
    var_dump ($data);
} catch (exception $exception) {
    var_dump ($exception);
}
print "</pre>";
</code>
</code>


== XML-RPC ==
== SOAP ==
[http://framework.zend.com/manual/en/zend.xmlrpc.client.html Zend XML-RPC client]
The Moodle SOAP server extends Zend SOAP Server. It is recommended to have a look to the [http://framework.zend.com/manual/en/zend.soap.client.html Zend SOAP client documentation].
Except the class name ''Zend_Soap_Client'', the entire example code would be similar to XML-RPC.


== REST ==
== REST ==
Look at the web service function documentation on your [https://docs.moodle.org/en/How_to_get_a_security_key security keys] page to know about the returned XML structure.


<code php>
//get the profile data of Moodle User
include 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::autoload('Zend_Loader');
$params = array(
    'userids' => array(2,4), // the params to passed to the function
    'wsfunction' => 'moodle_user_get_users_by_id',  // the function to be called
    'wstoken' => '8dc8f5ca046129706c0d85a56efcaddd' //token need to be passed in the url
);
//set web service url server
$serverurl = "http://{servername}/webservice/rest/server.php";
$client = new Zend_Http_Client($serverurl);
print "<pre>";
try {
    $client->setParameterPost($params);   
    $response = $client->request('POST');
    var_dump ($response);
} catch (exception $exception) {
    var_dump ($exception);
}
print "</pre>";
</code>




[[Category:Web Services]]
[[Category:Web Services]]

Latest revision as of 14:42, 22 April 2011

Template:Moodle 2.0

Moodle web service function documentation.jpg

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 moodle_group_create_groups.

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

include 'Zend/Loader/Autoloader.php'; Zend_Loader_Autoloader::autoload('Zend_Loader');

$function = "moodle_user_get_users_by_id";

// id of the users $params = array(array(2,4));

$serverurl = "http://{servername}/webservice/xmlrpc/server.php?wstoken=$token";

$client = new Zend_XmlRpc_Client($serverurl);

print "

";
try {
    $data = $client->call($function, $params);
    var_dump ($data);
} catch (exception $exception) {
    var_dump ($exception);
}
print "

";

SOAP

The Moodle SOAP server extends Zend SOAP Server. It is recommended to have a look to the Zend SOAP client documentation. Except the class name Zend_Soap_Client, the entire example code would be similar to XML-RPC.

REST

Look at the web service function documentation on your security keys page to know about the returned XML structure.

//get the profile data of Moodle User

include 'Zend/Loader/Autoloader.php'; Zend_Loader_Autoloader::autoload('Zend_Loader');

$params = array(

   'userids' => array(2,4), // the params to passed to the function
   'wsfunction' => 'moodle_user_get_users_by_id',   // the function to be called
   'wstoken' => '8dc8f5ca046129706c0d85a56efcaddd' //token need to be passed in the url

);

//set web service url server $serverurl = "http://{servername}/webservice/rest/server.php";

$client = new Zend_Http_Client($serverurl);

print "

";
try {
    $client->setParameterPost($params);    
    $response = $client->request('POST');

    var_dump ($response);
} catch (exception $exception) {
    var_dump ($exception);
}
print "

";