Note:

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

Web services API: Difference between revisions

From MoodleDocs
Line 42: Line 42:


== Example ==
== Example ==
Have a look at the [https://github.com/moodlehq/moodle-local_wstemplate web service template plugin]. It is a plugin containing a hello_world function. To make testing easy for you, the plugin is distributed with a test client in the folder /client.
You will find an example of the services.php file in the [https://github.com/moodlehq/moodle-local_wstemplate web service template plugin]. It is a plugin containing a web service hello_world function. To make testing easy for you, the plugin is distributed with a test client in the folder /client.
 
For more information on how to implement the external function read [https://docs.moodle.org/dev/Creating_a_web_service_and_a_web_service_function#Detailled_example How to create a web service function].


==See also==
==See also==


* [[Core APIs]]
* [[Core APIs]]

Revision as of 07:52, 16 January 2012

Overview

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.

Exposing a web service function is done in one file: services.php

services.php

  • This file is located in the db folder of your plugin
  • This file contains one or two array. The first array declares 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', // create this class in local/PLUGINNAME/externallib.php
               'methodname'  => 'FUNCTIONNAME', // 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'.
       )

);

  • The second array declare the pre-build services.

// 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
       )

);

Example

You will find an example of the services.php file in the web service template plugin. It is a plugin containing a web service hello_world function. To make testing easy for you, the plugin is distributed with a test client in the folder /client.

See also