source-toad / soapy
为 Laravel 构建的 Soap 客户端。
v3.0.0
2024-08-26 21:35 UTC
Requires
- php: ^8.2
- ext-simplexml: *
- ext-soap: *
- illuminate/support: ^11
Requires (Dev)
- laravel/pint: ^1.17
- orchestra/testbench: ^9.2
- phpunit/phpunit: ^11.2
README
SOAP 已过时,但仍在使用。Soapy 是现代化的,但功能有限,以适应我们的用例。
深受 artisaninweb/laravel-soap 启发。
安装
此包目前支持 Laravel 11.x。
composer require sourcetoad/soapy
此包将使用 Laravel 自动发现来自动注册服务提供者。
文档
选项
- wsdl - WSDL 的平面文件位置或 URL。
- trace - 是否在 SoapClient 上公开内部方法。
- cache - 用于 WSDL 缓存的标志。
- location - 覆盖用于 SOAP 请求的 URL。
- uri - 覆盖用于 SOAP 请求的命名空间。
- certificate - 用于与服务器进行身份验证的证书路径。
- options - 从 SoapClient#options 获取的任何设置数组。
- classmap - 对象到类的映射数组。
- typemap - 类型映射数组。(文档正在制作中)
示例(类映射)
创建客户端并使用类映射进行请求。
$this->client = SoapyFacade::create(function (SoapyCurtain $curtain) { return $curtain ->setWsdl('https://example.org?wsdl') ->setTrace(true) ->setOptions([ 'encoding' => 'UTF-8' ]) ->setClassMap([ 'Foo' => Foo::class, 'FooResponse' => FooResponse::class ]) ->setCache(WSDL_CACHE_MEMORY) ->setLocation('https://example.org'); });
假设你有如下预期的请求 XML。
<Foo> <bar>Connor</bar> <baz>true</baz> </Foo>
你可以生成一个匹配的类来表示该数据。
<?php class Foo { protected $bar; protected $baz; public function __construct(string $bar, bool $baz) { $this->bar = $bar; $this->baz = $baz; } }
然后可以在 SOAP 类上调用一个虚构的方法名“fizz”,如下所示。
$this->client->call('fizz', new Foo("Connor", true));
这显示了无需直接修改 XML 的好处。
同样对于响应。想象你收到了这个。
<FooResponse> <status>Success.</status> </FooResponse>
这也可以使用以下类进行映射。
<?php class FooResponse { protected $status; }
现在你可以这样做
echo $this->client->call('fizz', new Foo("Connor", true))->status; // Success.
示例(无类映射)
此示例显示了最低限度的 WSDL 位置和没有类映射。
$this->client = SoapyFacade::create(function (SoapyCurtain $curtain) { return $curtain ->setWsdl('https://example.org?wsdl') });
假设你有如下预期的请求 XML。
<Foo> <bar>Connor</bar> <baz>true</baz> </Foo>
然后可以在 SOAP 类上调用一个虚构的方法名“fizz”,如下所示。
$this->client->call('fizz', [ 'bar' => 'Connor', 'baz' => true });
由于人为错误,这更容易出错,但工作量较小。
示例(自定义客户端)
有时你可能有一个不愉快的 SOAP 集成。它可能使用默认的 SoapyClient 无法处理的某种东西。这是可以的,因为修补通用 SOAP 客户端以应对可能发生的所有奇怪情况是不切实际的。
从一个新类开始,该类扩展了我们的 SoapyBaseClient。
<?php class CustomClass extends \Sourcetoad\Soapy\SoapyBaseClient { // }
从该类开始,你可以覆盖它提供的任何功能。
然后在生成时传递类名(完全限定名)给幕布。
$this->client = SoapyFacade::create(function (SoapyCurtain $curtain) { return $curtain ->setWsdl('https://example.org?wsdl') }, CustomClass::class);