goetas-webservices / soap-server
纯PHP实现的SOAP 1.1和1.2服务器
Requires
- php: ^7.2
- goetas-webservices/soap-common: ^0.2
- jms/serializer: ^1.2|^2.0|^3.0
- php-http/discovery: ^1.13
- psr/http-factory: ^1.0
- psr/http-factory-implementation: ^1.0
- psr/http-message: ^1.0
- psr/http-message-implementation: ^1.0
- psr/http-server-handler: ^1.0
- psr/log: ^1.0
- rybakit/arguments-resolver: ^0.5.0
- symfony/dependency-injection: ^3.3|^4.0|^5.0
Requires (Dev)
- doctrine/coding-standard: ^8.1
- goetas-webservices/wsdl2php: ^0.5.1
- guzzlehttp/psr7: ^1.3
- laminas/laminas-diactoros: ^2.4
- phpunit/phpunit: ^7.0|^8.0
This package is auto-updated.
Last update: 2024-08-28 11:50:31 UTC
README
PHP实现SOAP 1.1和1.2服务器规范。
优势
- 纯PHP,不依赖于
ext-soap
- 可扩展(支持JMS事件监听器)
- PSR-7 HTTP消息
- PSR-15 HTTP服务器处理程序
- 生产环境中不解析WSDL/XSD
- 支持IDE类型提示
仅支持document/literal样式,且web服务应遵循WS-I指南。
没有计划支持已弃用的rpc和encoded样式。不遵循WS-I规范的服务可能工作,但官方不支持。
演示
goetas-webservices/soap-server-demo是一个演示项目,展示了如何在通用的PHP Web应用程序中创建SOAP服务器。
安装
推荐使用Composer安装goetas-webservices / soap-server。
将此包添加到您的composer.json
文件中。
{
"require": {
"goetas-webservices/soap-server": "^0.1",
},
"require-dev": {
"goetas-webservices/wsdl2php": "^0.5.1",
},
}
如何使用
为了提高性能,此库基于所有SOAP/WSDL元数据都必须编译成PHP兼容元数据的概念(实际上是一个大型的普通PHP数组,因此非常快)。
为此,我们需要定义一个配置文件(在本例中称为config.yml
),其中包含一些重要信息。
以下是一个示例
# config.yml soap_server: namespaces: 'http://www.example.org/test/': 'TestNs/MyApp' destinations_php: 'TestNs/MyApp': soap/src destinations_jms: 'TestNs/MyApp': soap/metadata aliases: 'http://www.example.org/test/': MyCustomXSDType: 'MyCustomMappedPHPType' metadata: 'test.wsdl': ~
此文件包含一些重要部分
WSDL特定
metadata
指定了放置用于生成所有所需PHP元数据的WSDL文件的位置。
XML/XSD特定
-
namespaces
(必需)定义了XML命名空间与PHP命名空间之间的映射。(在示例中,http://www.example.org/test/
XML命名空间映射到TestNs\MyApp
) -
destinations_php
(必需)指定了保存属于TestNs\MyApp
PHP命名空间类的目录。(在本例中,TestNs\MyApp
类将保存到soap/src
目录。 -
destinations_jms
(必需)指定了保存属于TestNs\MyApp
PHP命名空间JMS序列化器元数据文件的目录。(在本例中,TestNs\MyApp
元数据将保存到soap/metadata
目录。 -
aliases
(可选)指定了一些由自定义JMS序列化器处理程序处理的映射。允许指定不生成某些XML类型的元数据,并将它们直接分配给PHP类。为此PHP类需要创建一个自定义JMS序列化/反序列化处理程序。
元数据生成
为了能够使用SOAP服务器,我们必须生成一些元数据和PHP类。
为此,我们可以运行
bin/soap-server generate \ tests/config.yml \ --dest-class=GlobalWeather/Container/SoapServerContainer \ soap/src-gw/Container
bin/soap-server generate
是我们运行的命令tests/config.yml
是我们配置文件的路径--dest-class=GlobalWeather/Container/SoapServerContainer
允许指定将持有所有web服务元数据的容器类的完全限定名称。soap/src/Container
是保存包含所有web服务元数据的容器类的路径(您需要配置自动加载器来加载它)
使用服务器
一旦所有元数据都已生成,我们就可以使用我们的SOAP服务器。
让我们看一个最小示例
// composer auto loader require __DIR__ . '/vendor/autoload.php'; // instantiate the main container class // the name was defined by --dest-class=GlobalWeather/Container/SoapServerContainer // parameter during the generation process $container = new SoapServerContainer(); // create a JMS serializer instance $serializer = SoapContainerBuilder::createSerializerBuilderFromContainer($container)->build(); // get the metadata from the container $metadata = $container->get('goetas_webservices.soap.metadata_reader'); $handler = new class() { function anAction($someParam) { return 'OK 123'; } function someAction($someParam, HeadersIncoming $headersIncoming) { $headers = $headersIncoming->getRawheader(); // perform some checks on $headers here return 'OK 123'; } function anotherAction($someParam, HeadersOutgoing $headersOutgoing) { // reply with custom headers $headersOutgoing->addHeader(new Header(new SomeHeaderData())); // reply with custom headers in pure xml $dom = new DOMDocument(); $dom->appendChild($dom->createElement('foo', 'bar')); $headersOutgoing->addHeader(new Header($dom->documentElement)); return 'OK 456'; } function someErrAction($someParam) { throw new CustomExcpetion(); // converted in a soap fault } }; $router = new DefaultRouter(new ConfiguredRoute($handler)); $factory = new ServerFactory($metadata, $serializer, $router); // get the soap server $server = $factory->getServer('test.wsdl'); // create psr7 request $request = \Laminas\Diactoros\ServerRequestFactory::fromGlobals(); // let the server handle the request $response = $server->handle($request); // send the response to the client (using laminas/laminas-httphandlerrunner) $emitter = new \Laminas\HttpHandlerRunner\Emitter\SapiEmitter(); $emitter->emit($response);
注意
本项目中提供的代码遵循MIT许可证。如需专业支持,请联系goetas@gmail.com或访问https://www.goetas.com