Web服务API

Web服务API

 
 

概观

Web服务API允许您将插件的功能(通常是外部函数公开为Web服务。

一旦你完成了这个工作,你的插件的功能就可以被其他系统通过Web服务使用XML-RPC,REST或者SOAP之类的许多协议来访问。

作为Web服务函数公开函数是在一个名为services.php的文件中完成的。

services.php

  • 这个文件可以添加到你的插件数据库子文件夹。 (plugin/db/services.php)
  • 这个文件包含一个或两个数组。第一个数组声明您的Web服务功能。这些声明中的每一个都引用模块中的函数(通常是外部函数)。
$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'.
                'ajax'        => true, // true/false if you allow this web service function to be callable via ajax
                'capabilities'  => 'moodle/xxx:yyy, addon/xxx:yyy',  // List the capabilities used in the function (missing capabilities are displayed for authorised users and also for manually created tokens in the web interface, this is just informative).
                'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)    // Optional, only available for Moodle 3.1 onwards. List of built-in services (by shortname) where the function will be included. Services created manually via the Moodle interface are not supported.
        )
);
  • 第二个可选数组声明了预建的服务。
// 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
        )
);

每当services.php更改时,不要忘记在插件的version.php文件中增加版本号,否则Moodle将无法检测到更改。

详细的教程

这个系统的更详细的教程可以在下面的页面找到:

除此之外,该教程还介绍了如何定义函数的参数和返回值。

您将在Web服务模板插件中找到services.php文件的示例这个插件包含一个web服务hello_world函数。

另外,为了使测试变得容易,插件与客户文件夹中的测试客户端一起分发

 

猜你喜欢

转载自www.cnblogs.com/lichihua/p/8396290.html