jelix / webservice-helper
从类生成WSDL内容
Requires
- php: >=5.6
- ext-soap: *
Requires (Dev)
- phpunit/phpunit: 8.5.*
This package is auto-updated.
Last update: 2024-08-28 19:32:13 UTC
README
这是David Kingma的Webservice Helper的分支。原始源代码来自www.jool.nl/webservicehelper/。原始作者:David Kingma davidjoolnl
此分支的维护者:Laurent Jouanneau 网站地址:https://github.com/jelix/webservice-helper
webservice helper有什么作用?
webservice helper的作用正如其名:帮助您将PHP类作为webservice使用。它生成文档、webservice描述文件(WSDL)并处理错误处理。它由三个部分组成
- 扩展PHP反射类以解析注释中的参数信息和返回值信息。文档和WSDL都是从这些类生成的。(也请参见document.php作为示例)
- 扩展PHP SOAP实现。它捕获所有正常异常并允许在webservice方法中使用类型提示。(例如
saveContact(contact $contact)
)
安装
使用Composer安装库。包名:jelix/webservice-helper
。
如果您不想使用Composer,您可以复制源代码的内容(所有必要的类都位于lib/
中),并且您必须创建一个自动加载器(如example/common.php
或您必须包含所有类)。
手册
那么您如何创建自己的webservice。作为一个示例,我们创建一个添加和显示联系人的webservice。首先,在/example/data_objects
中创建一个名为contactManager的类,包含公开函数getContacts()
、saveContact(contact $contact)
和newContact()
。为了让Webservice helper知道每个方法的参数和返回值,我们在每个方法前放置一个注释,指定参数和返回类型。例如
/** * This method saves the given contact * @return contact[] Array with all the contacts */ public function getContacts(){} /** * This method saves the given contact * @param contact The contact to save * @return void */ public function saveContact(contact $contact){} /** * This method saves the given contact * @return contact A new contact template */ public function newContact(){} /** * Gets the current contact list as associative array * @return contact[=>] keys are contact name */ public function getContactsAsAssoc() {}
由于newContact()
和getContacts()
使用contact类型作为返回值,因此我们需要定义contact的样子。为此,我们创建一个名为contact的类
class contact{ /** @var string */ public $name; /** @var string */ public $address; }
由于字符串(就像布尔型和int)是已知的数据类型,因此我们不需要进一步指定。
完成我们webservice的最后一件事是告诉webservice,contactManager
类是一个允许的webservice,而contact
是一个允许的数据结构(用于文档目的和类映射)。在config.php
中,将"contactmanager"
添加到WSClasses
数组中,并将"contact"
添加到WSStructures
数组中。
现在您可以在/doc/documentation.php
查看服务文档,以及在/example/service.php?class=contactManager&wsdl
查看wsdl。
关于包含对象的关联数组的说明:如果SOAP服务器定义了类映射(PHP中SoapServer
的'classmap'
选项),则对象将被包裹在SoapVal
对象中,否则它们将是简单的stdClass
对象。
常见问题解答
-
我的函数在文档或WSDL文件中未显示?请检查它是否是一个公开函数,并且不以
__
开头。 -
它不起作用!
- 您在生成的文档中看到任何警告吗?修复它们
- 检查类名的大小写敏感性
- 您检查了JavaScript控制台以查看是否有任何问题吗?
- 尝试清理WSDL缓存目录中的WSDL缓存吗?
- 您在客户端检查了WSDL URL吗?
-
我能在自己的项目中使用webservice helper吗?是的,你可以在LGPL 2.1的条款下使用它
-
我能贡献吗?
是的,在https://github.com/jelix/webservice-helper上创建一个issue和/或一个PR
示例和单元测试
见tests/README.md