Creating a web service and a web service function: Difference between revisions
No edit summary |
|||
Line 10: | Line 10: | ||
== declare your web service functions == | == declare your web service functions == | ||
In the db/services.php of each component, is a structure something like this to describe the web service functions, and optionally, any larger services built up of several functions. | |||
<code php> | |||
$functions = array( | |||
'moodle_user_create_users' => array( //web service name (unique in all Moodle) | |||
'classname' => 'moodle_user_external', //class containing the function implementation | |||
'methodname' => 'create_users', //name of the function into the class | |||
'classpath' => 'user/externallib.php', //file containing the class (only used for core external function, not needed if your file is 'component/externallib.php'), | |||
'description' => 'create some users', | |||
'type' => 'write' | |||
) | |||
); | |||
$services = array( | |||
'servicename' => array( | |||
'functions' => array ('functionname', 'secondfunctionname'), //web service function name | |||
'requiredcapability' => 'some/capability:specified', | |||
'restrictedusers' = >1, | |||
'enabled'=>0, //used only when installing the services | |||
) | |||
); | |||
</code> | |||
The function name is arbitrary, but it must be globally unique so we highly recommend using the component name as prefix (and "moodle" for core functions). | |||
The actual param description and description of returned values can be obtained from the same class by static method calls by adding '_parameters' and '_returns' to the methodname value. | |||
== declare your web service == | == declare your web service == | ||
Even for security purpose, you should avoid to define a service In some case you will want to force group of web service. | Even for security purpose, you should avoid to define a service In some case you will want to force group of web service. |
Revision as of 07:28, 23 November 2009
Moodle 2.0
First you need to read and execute the administration manual Setting_up_a_web_service. Now you should be more familiar about how web services are set into Moodle.
Preparation
Identify:
- core function(s) you need to call (or/and write)
- the param types that new to be send to your web service function
- the returned value types
- the user capabilities (it most of the time the ones logically required by the core functions)
declare your web service functions
In the db/services.php of each component, is a structure something like this to describe the web service functions, and optionally, any larger services built up of several functions.
$functions = array(
'moodle_user_create_users' => array( //web service name (unique in all Moodle)
'classname' => 'moodle_user_external', //class containing the function implementation
'methodname' => 'create_users', //name of the function into the class
'classpath' => 'user/externallib.php', //file containing the class (only used for core external function, not needed if your file is 'component/externallib.php'),
'description' => 'create some users',
'type' => 'write'
)
);
$services = array(
'servicename' => array(
'functions' => array ('functionname', 'secondfunctionname'), //web service function name
'requiredcapability' => 'some/capability:specified',
'restrictedusers' = >1,
'enabled'=>0, //used only when installing the services
)
);
The function name is arbitrary, but it must be globally unique so we highly recommend using the component name as prefix (and "moodle" for core functions).
The actual param description and description of returned values can be obtained from the same class by static method calls by adding '_parameters' and '_returns' to the methodname value.
declare your web service
Even for security purpose, you should avoid to define a service In some case you will want to force group of web service.