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
No edit summary
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.
= Officially supported protocols=
= Officially supported protocols=


* '''REST''': the current REST Moodle server use POST paramters and XML. The 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 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.
* '''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 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
* '''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 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].
* '''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 is currently work in progress, see the issue tracker MDL-20808.
* '''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''.
 
You will also look for functions documentation. A documentation generator will be available. Please follow the tracker issue MDL-20803


== What protocol to use==
= PHP client examples =
*PHP:  have a look at [http://framework.zend.com/manual/en/zend.soap.client.html SOAP]/[http://framework.zend.com/manual/en/zend.xmlrpc.client.html XMLRPC] zend clients. SOAP/XMLRPC Moodle server use Zend server, it should be easy to use.
*Java/.Net: use the REST protocol. The SOAP server doesn't generate a Java compliant WSDL yet.
*AS3: use the AMF protocol. Moodle use Zend AMF server. This server is supported by Zend and Adobe.


= Authentication =
== SOAP ==
There is two methods to be authenticated by the web service. Each time you call a web service function:
[http://framework.zend.com/manual/en/zend.soap.client.html Zend SOAP client]
# you pass your username/password.
# you pass a token. This token has been generated for you by an administrator into the Moodle administration.


== username/password authentication==
if you look to the Moodle test client PHP code, you will notice that username/password are sent as GET parameters.
<code php>
<code php>
//wwwroot/webservice/$protocol/simpleserver.php?wsusername=your_username&wspassword=your_password
$function = 'the_function_name'; //see the web service documentation
$serverurl = "$CFG->wwwroot/webservice/$protocol/simpleserver.php";
    $params = array('search' => $search, 'downloadable' => $downloadable,
$serverurl .= '?wsusername='.urlencode($data->wsusername);
        'enrollable' => !$downloadable, 'options' => $options);
$serverurl .= '&wspassword='.urlencode($data->wspassword);
    $serverurl = $huburl . "/local/hub/webservice/webservices.php";
    require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
    $xmlrpcclient = new webservice_xmlrpc_client($serverurl, $token);
    try {
        $result = $xmlrpcclient->call($function, $params);
        $courses = $result['courses'];
        $coursetotal = $result['coursetotal'];
    } catch (Exception $e) {
        $errormessage = $OUTPUT->notification(
                        get_string('errorcourselisting', 'block_community', $e->getMessage()));
    }
</code>
</code>


== token authentication==
== XML-RPC ==
if you look to the Moodle test client PHP code, you will notice that username/password are sent as GET parameters.
[http://framework.zend.com/manual/en/zend.xmlrpc.client.html Zend XML-RPC client]
<code php>
 
//wwwroot/webservice/$protocol/simpleserver.php?wstoken=your_token
== REST ==
$serverurl = "$CFG->wwwroot/webservice/$protocol/server.php";
 
$serverurl .= '?wstoken='.urlencode($data->token);
 
</code>


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

Revision as of 07:28, 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 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.
  • SOAP: the current SOAP server is based on the Zend SOAP sever (itself based on the PHP SOAP sever). Zend publishes a Zend SAOP 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 current XML-RPC server is based on Zend XML-RPC server. Zend also publishes 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.

PHP client examples

SOAP

Zend SOAP client

$function = 'the_function_name'; //see the web service documentation

   $params = array('search' => $search, 'downloadable' => $downloadable,
       'enrollable' => !$downloadable, 'options' => $options);
   $serverurl = $huburl . "/local/hub/webservice/webservices.php";
   require_once($CFG->dirroot . "/webservice/xmlrpc/lib.php");
   $xmlrpcclient = new webservice_xmlrpc_client($serverurl, $token);
   try {
       $result = $xmlrpcclient->call($function, $params);
       $courses = $result['courses'];
       $coursetotal = $result['coursetotal'];
   } catch (Exception $e) {
       $errormessage = $OUTPUT->notification(
                       get_string('errorcourselisting', 'block_community', $e->getMessage()));
   }

XML-RPC

Zend XML-RPC client

REST