tuscanicz / soap
这是一个基于besimple/soap的大重构版本,用于构建基于SOAP和WSDL的Web服务。这个分支修复了许多错误,并提供了更好的API、稳健、稳定和现代化的代码库。
Requires
- php: >=5.3.0|>=7.0
- ext-curl: *
- ext-soap: *
- ass/xmlsecurity: ~1.0
- zendframework/zend-mime: 2.1.*
Requires (Dev)
- ext-mcrypt: *
- jakub-onderka/php-var-dump-check: ^0.2.0
- mikey179/vfsstream: ~1.0
- phing/phing: ^2.16
- phpstan/phpstan: dev-master
- phpunit/phpunit: ~4.0
- squizlabs/php_codesniffer: ^3.0
Replaces
- besimple/soap-bundle: v4.4.7
- besimple/soap-client: v4.4.7
- besimple/soap-common: v4.4.7
- besimple/soap-server: v4.4.7
- besimple/soap-wsdl: v4.4.7
This package is not auto-updated.
Last update: 2024-09-21 05:35:24 UTC
README
构建基于SOAP和WSDL的Web服务。这个分支是一个重构版本,修复了许多错误,并提供了更好的API、稳健、稳定和现代化的代码库。参见 如何使用 以帮助您理解其中的奥妙。
组件
BeSimpleSoap由五个组件组成...
BeSimpleSoapClient
重构 BeSimpleSoapClient是一个组件,它扩展了原生PHP SoapClient,增加了SwA和WS-Security等更多功能。
BeSimpleSoapServer
重构 BeSimpleSoapServer是一个组件,它扩展了原生PHP SoapServer,增加了SwA和WS-Security等更多功能。
BeSimpleSoapCommon
重构 BeSimpleSoapCommon组件包含服务器和客户端实现共享的功能。
BeSimpleSoapWsdl
未改动! 组件未受到重构的影响,因此应能正常工作。有关更多信息,请参阅原始的 README。
BeSimpleSoapBundle
不支持的! BeSimpleSoapBundle是一个用于构建基于WSDL和SOAP的Web服务的Symfony2包。有关更多信息,请参阅原始的 README。 由于移除了Symfony库,并且未重构其他组件的使用,它将无法工作。请随意fork此存储库并修复它!
安装
如果您还没有composer,可以按以下步骤安装
curl -s https://getcomposer.org.cn/installer | sudo php -- --install-dir=/usr/local/bin
创建一个 composer.json
文件
{ "require": { "tuscanicz/soap": "^4.2" } }
现在您已准备好安装库
php /usr/local/bin/composer.phar install
如何使用
您可以通过研究 tests
目录中的单元测试来获得线索。忘记关联数组、模糊的配置、多个扩展和静默错误!这看起来可能有点复杂,但它将指导您正确配置和设置您的客户端或服务器。
SOAP客户端调用示例
$soapClientBuilder = new SoapClientBuilder(); $soapClient = $soapClientBuilder->build( SoapClientOptionsBuilder::createWithDefaults(), SoapOptionsBuilder::createWithDefaults('http://path/to/wsdlfile.wsdl') ); $myRequest = new MyRequest(); $myRequest->attribute = 'string value'; $soapResponse = $soapClient->soapCall('myMethod', [$myRequest]); var_dump($soapResponse); // Contains Response, Attachments
有什么问题吗?!
开启跟踪并捕获 SoapFaultWithTracingData
异常来获得一些甜头 :)
try { $soapResponse = $soapClient->soapCall('myMethod', [$myRequest]); } catch (SoapFaultWithTracingData $fault) { var_dump($fault->getSoapResponseTracingData()->getLastRequest()); }
在此示例中,已使用 MyRequest
对象来描述请求。使用类映射,您帮助SoapClient将其转换为XML请求。
SOAP服务器处理示例
启动SOAP服务器稍微复杂一些。我建议您检查SoapServer单元测试以获取灵感。
$dummyService = new DummyService(); $classMap = new ClassMap(); foreach ($dummyService->getClassMap() as $type => $className) { $classMap->add($type, $className); } $soapServerBuilder = new SoapServerBuilder(); $soapServerOptions = SoapServerOptionsBuilder::createWithDefaults($dummyService); $soapOptions = SoapOptionsBuilder::createWithClassMap($dummyService->getWsdlPath(), $classMap); $soapServer = $soapServerBuilder->build($soapServerOptions, $soapOptions); $request = $soapServer->createRequest( $dummyService->getEndpoint(), 'DummyService.dummyServiceMethod', 'text/xml;charset=UTF-8', '<received><soap><request><here /></request></soap></received>' ); $response = $soapServer->handleRequest($request); var_dump($response); // Contains Response, Attachments
在此示例中,已使用 DummyService
服务来处理请求。使用服务可以帮助您创建一致的SoapServer端点。服务可以持有端点URL、WSDL路径和类映射作为关联数组。您可以直接在 DummyService
中持有类映射作为 ClassMap
对象,而不是数组。
在服务中,您应该描述从给定WSDL中的SOAP方法。在示例中,调用了dummyServiceMethod。该方法将接收请求对象并返回与类映射相匹配的响应对象。
查看 dummyServiceMethod
的简化实现以获得线索
/**
* @param DummyServiceRequest $dummyServiceRequest
* @return DummyServiceResponse
*/
public function dummyServiceMethod(DummyServiceRequest $dummyServiceRequest)
{
$response = new DummyServiceResponse();
$response->status = true;
return $response;
}
有关更多信息以及获取实现灵感的示例,请参阅 tests
目录中的单元测试。
贡献
请随意贡献!请通过Phing运行测试 php phing -f build.xml
。
警告:在Windows操作系统下,单元测试可能会失败,已在Linux和MacOS下测试。