gpt4 book ai didi

php - SoapVar 和 SoapParam 有什么区别?

转载 作者:可可西里 更新时间:2023-11-01 12:37:57 25 4
gpt4 key购买 nike

在 PHP 中,我们有 SoapVarSoapParam 类。我已经困惑很久了,因为在 php.net 上没有关于它们的合适文档。

今天我惊讶地发现这些行将在 XML 输入中产生完全相同的结果:

$soapVar   = new SoapVar((object) ['Foo' => 'bar'], null, null, null, 'TagName');
$soapParam = new SoapParam((object) ['Foo' => 'bar'], 'TagName');

在大多数 SoapClient 教程中,每当有人想要设置自定义 xsi:type< 时,我都会在 SoapVar 中看到带有 SoapParam/:

$response = $soapClient->DoSomething(
new SoapParam(
new SoapVar(
(object) ['Foo' => 'bar'],
null,
'TypeName'
),
'TagName'
)
);

这真的很不直观,因为 SoapVarSoapParam 类名并没有说明太多。同样的结果可以通过更优雅的方式实现:

$response = $soapClient->DoSomething(
new SoapVar(
(object) ['Foo' => 'bar'],
null,
'TypeName',
null,
'TagName'
)
);

SoapParam 的作用是什么?它只是更简单的 SoapVar 版本吗?看来这两个人很困惑和误解。 SoapParam 是否有任何额外的行为?

最佳答案

你是对的,手册在解释理论上的确切差异时有点含糊,我很惊讶直到现在还没有人指出一些详细信息或解释它们的差异。

根据手册

SOAP 参数:

Represents parameter to a SOAP call.

SOAP 水:

A class representing a variable or object for use with SOAP services.

我往下看了一点,我能找到的主要区别是它们的代表性构造函数中的 data 参数:

对于 SoapParam::SoapParam(混合 $data,字符串 $name)其中 $data =

The data to pass or return. This parameter can be passed directly as PHP value, but in this case it will be named as paramN and the SOAP service may not understand it.

而另一方面,我们有 SoapVar::SoapVar ( mixed $data , string $encoding [, string $type_name [, string $type_namespace [, string $node_name [, string $node_namespace ]]]] ) 其中 $data =

The data to pass or return.

通过考虑这两个因素,即 PHP 手册中的示例,我相信很好地指出了参数传递的主要区别,以及一个可以做什么而另一个不能做什么。

在 SoapParam 的情况下

<?php
$client = new SoapClient(null,array('location' => "http://localhost/soap.php",
'uri' => "http://test-uri/"));
$client->SomeFunction(new SoapParam($a, "a"),
new SoapParam($b, "b"),
new SoapParam($c, "c"));
?>

其中 $a$b$c 是直接发送的 PHP 变量,无需通过结构/类传递它们。 SaopParam 构造函数正在获取参数 $data(PHP 变量)和代表参数名称的字符串。

在另一边SoapVar

<?php
class SOAPStruct {
function SOAPStruct($s, $i, $f)
{
$this->varString = $s;
$this->varInt = $i;
$this->varFloat = $f;
}
}
$client = new SoapClient(null, array('location' => "http://localhost/soap.php",
'uri' => "http://test-uri/"));
$struct = new SOAPStruct('arg', 34, 325.325);
$soapstruct = new SoapVar($struct, SOAP_ENC_OBJECT, "SOAPStruct", "http://soapinterop.org/xsd");
$client->echoStruct(new SoapParam($soapstruct, "inputStruct"));
?>

如您所见,参数/变量通过每个定义的类传递,并且在该类的对象作为参数传递之后。该类是 SOAP 结构的代表(SOAP 可以通过 SoapParam 识别/排除风险)。

现在是几乎令人困惑的部分...尽管构造函数中存在一些差异,但为什么在您的示例中有相同的行为?

$soapVar   = new SoapVar((object) ['Foo' => 'bar'], null, null, null, 'TagName');
$soapParam = new SoapParam((object) ['Foo' => 'bar'], 'TagName');

我并不感到惊讶,如上所述,SOAP 结构或 CORRECT Soap 结构的 PHP 变量的数据是相同的:)感谢面向对象,我们传递了由 PHP 类生成的对象。在 SoapVar 中,对象的构造具有更多选项,从而允许用户针对 SOAP 调用调整更“安全”的请求。在 SoapParam 上,可以通过传递任何对象及其名称来构造对象。

您的 (object) ['Foo' => 'bar'] 非常简单,不需要 XML 模式编码,因此接收它的 SOAP 调用我相信可以很容易地理解它。它不需要

  • encoding

The encoding ID, one of the XSD_... constants.

这是 SoapVar 构造函数中的第二个参数。

最后,因为他们都接受一个对象(任何对象)来代替 $data 变量,据我所知,SoapVar 只是提供了构建更安全的请求以供 SOAP 调用理解的可能性通过构造函数中的额外参数。 SoapParam 只是一个坏蛋,它允许您发送任何您喜欢的东西,冒着 SOAP 拒绝它的风险……就像几乎想充当 JSON @ RESTful :P 不关心它发送的数据的类型和结构:)

引用资料:

关于php - SoapVar 和 SoapParam 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42798881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com