sf4 / wsdl-creator
使用PHPdoc(注释、反射)的PHP WSDL生成器。
Requires
- letsdrink/ouzo-goodies: 1.5.0
Requires (Dev)
- phpunit/phpunit: 4.7.5
- satooshi/php-coveralls: dev-master
- symfony/console: v2.7.1
README
简介
WSDL生成器允许基于PHP类使用注解和反射机制生成WSDL文档。此生成器还提供了生成概述方法和参数以及WSDL中使用的SOAP示例的可能性。
支持:>= PHP7.2。
安装
要安装wsdl-creator到您的项目中,请使用 composer。
###创建新项目
composer.phar create-project georgo/wsdl-creator myproject
###或在composer.json文件中添加
require: {
"sf4/wsdl-creator": "^2"
}
配置
要开始使用生成器,您必须创建一个新的 WSDLCreator
对象并为其定义
- 要生成
WSDL
的类 SOAP
服务器位置- 文档命名空间。
$wsdl = new WSDL\WSDLCreator('ClassName', 'https:///wsdl-creator/ClassName.php'); $wsdl->setNamespace("http://foo.bar/");
SOAP
服务器必须在 WSDLCreator
中指定的位置创建。
$server = new SoapServer(null, array( 'uri' => 'https:///wsdl-creator/ClassName.php' )); $server->setClass('ClassName'); $server->handle();
要渲染 XML
,请使用方法 renderWSDL
。要正确加载生成器类,请使用位于 vendor/autoload.php
的composer加载器。
完整的配置列表
require_once 'vendor/autoload.php'; use WSDL\WSDLCreator; if (isset($_GET['wsdl'])) { $wsdl = new WSDL\WSDLCreator('ClassName', 'https:///wsdl-creator/ClassName.php'); $wsdl->setNamespace("http://foo.bar/"); $wsdl->renderWSDL(); exit; } $server = new SoapServer(null, array( 'uri' => 'https:///wsdl-creator/ClassName.php' )); $server->setClass('ClassName'); $server->handle();
现在,如果我们尝试调用地址 https:///wsdl-creator/ClassName.php?wsdl
,您将收到 WSDL
文档。
定义网络服务方法
要定义网络服务方法,您必须使用 @WebMethod
注解。
简单类型
简单类型在此处描述。
###用法
/**
* @param string $name
*/
因此,您输入 @param
后跟一个简单类型(string
)和变量的名称($name
)。
您还可以使用简单类型的数组。
###用法
/**
* @param string[] $names
*/
现在,在输入参数中,您必须定义传递数组的类型(string[]
)。
###示例
###注解
- @desc 方法描述
- @param 类型 $varialbe_name
- @return 类型 $return
包装类型
包装类型是用户定义的类,您可以使用它们生成WSDL
复杂类型。
###用法
/**
* @param wrapper $user @className=User
*/
您必须定义一个名为 User
的类,其中包含公共字段和包含字段类型的文档注释,例如
class User { /** * @type string */ public $name; /** * @type int */ public $age; }
此机制使用 反射,即 User
类必须对生成类可见 - 可能使用 命名空间(\Namespace\To\User
)。
您可以定义包装器的数组。
###用法
/**
* @return wrapper[] $users @className=User
*/
此注解将生成用户数组。
###示例
###注解
- @desc 方法描述
- @param 包装器[] @className=ClassName
- @return 包装器 @className=\Namespace\To\ClassName
对象类型
您可以在运行时动态创建对象。使用 object
参数类型。
###用法
/**
* @param object $info @string=$name @int=$age
*/
此注解创建具有 name
和 age
元素的复杂类型。
您还可以将类包装在对象中。
###用法
/**
* @return object $userNameWithId @(wrapper $user @className=User) @int=$id
*/
上述示例将返回包含指向 User
复杂类型和 int
类型的指针的 UserNameWithId
对象。
另一个选项是创建对象数组。
###用法
/**
* @param object[] $payments @float=$payment @string=$user
*/
此注解创建具有每个元素上的 payment
和 user
的对象数组。
###示例
###其他信息
在 object
中,您可以使用包装器数组和简单类型数组。
###注解
- @desc 方法描述
- @param 对象 $object @type1=$name1 @type2=$name2
- @param 对象 $object @(包装器 $nameWrapper @className=User) @type1=$name1
- @param 对象[] $object @type1[]=$name1
- @return
服务概述
您可以通过 renderWSDLService
方法生成服务概述。这将显示有关服务中使用的所有方法和参数的信息以及示例 SOAP
请求。
###示例
样式
默认情况下,WSDLCreator
将使用 rpc/literal 绑定样式生成 WSDL。
要指定rpc/encoded或包装的document/literal绑定风格,请将绑定风格设置在WSDLCreator
对象上。
$wsdl = new WSDL\WSDLCreator('ClassName', 'https:///wsdl-creator/ClassName.php'); @wsdl->setBindingStyle(new WSDL\XML\Styles\RpcEncoded()); $wsdl->setNamespace("http://foo.bar/");
当指定包装的document/literal绑定风格时,您可以使用WSDL\DocumentLiteralWrapper
来自动将返回值包装在合适的包装对象中。
require_once 'vendor/autoload.php'; use WSDL\DocumentLiteralWrapper; use WSDL\WSDLCreator; use WSDL\XML\Styles\DocumentLiteralWrapped; if (isset($_GET['wsdl'])) { $wsdl = new WSDLCreator('ClassName', 'https:///wsdl-creator/ClassName.php'); @wsdl->setBindingStyle(new DocumentLiteralWrapped()); $wsdl->setNamespace("http://foo.bar/"); $wsdl->renderWSDL(); exit; } ini_set('soap.wsdl_cache_enabled', '0'); $server = new SoapServer('https:///wsdl-creator/ClassName.php?wsdl', array( 'style' => SOAP_DOCUMENT, 'use' => SOAP_LITERAL, )); $server->setObject(new DocumentLiteralWrapped(new ClassName())); $server->handle();