Web services API
The Web services API allows you to expose particular functions in your module (usually external functions) as web services.
Once you have done this, your functions will be available to admins to configure as part of their Web services over a given protocol like XML-RPC, REST or SOAP.
You must create two files to expose web service functions:
- plugin/db/services.php - the declaration of your web service functions
- plugin/externallib.php - the functions implementation
- This file is located in the db folder of your plugin
- It contains an array contains declaring your web service functions. Each of these declarations reference an external function.
<?php $functions = array( 'local_PLUGINNAME_FUNCTIONNAME' => array( // local_PLUGINNAME_FUNCTIONNAME is the name of the web service function that the client will call. 'classname' => 'local_PLUGINNAME_external', // you must create this class in local/PLUGINNAME/externallib.php 'methodname' => 'FUNCTIONNAME', // you must implement this function into the above class 'classpath' => 'local/PLUGINNAME/externallib.php', 'description' => 'This documentation will be displayed in the generated API documentation (Administration > Plugins > Webservices > API documentation)', 'type' => 'write', // the value is 'write' if your function does any database change, otherwise it is 'read'. ) ); // OPTIONAL // During the plugin installation/upgrade, Moodle installs these services as pre-build services. // A pre-build service is not editable by administrator. $services = array( 'MY SERVICE' => array( 'functions' => array ('local_PLUGINNAME_FUNCTIONNAME'), 'restrictedusers' => 0, // if 1, the administrator must manually select which user can use this service. // (Administration > Plugins > Web services > Manage services > Authorised users) 'enabled'=>1, // if 0, then token linked to this service won't work ) );
- This file is located at the root of your plugin.
- This file is composed by a class that contains the external function referenced in services.php
Have a look to the web service template. It is a simple hello_world function. It also contains a test client in the folder /client.