PHP的SoapVar没有设置属性 [英] php SoapVar not setting attributes

查看:77
本文介绍了PHP的SoapVar没有设置属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向肥皂请求添加一些属性.在PHP.net上评分最高的评论( http://php.net/manual/zh/soapvar.soapvar.php )和此处 ="https://stackoverflow.com/questions/7223896/php-soapvar-object-attribute">此处都说相同的话:

I'm trying to add a few attributes to a soap request. Top rated comment on PHP.net (http://php.net/manual/en/soapvar.soapvar.php) and here and here on SO all say the same thing:

$param = array(
    "_" => 'value',
    'attrName' => 'attributeName'
);
$p = new SoapVar($param, SOAP_ENC_OBJECT);

应该返回

<param attrName="attributeName">value</param>

那将是很棒的,除了当我运行该代码块时,我会得到以下XML:

Which would be great, except when I run that block of code, I get this XML:

<param>
    <_>value</_>
    <attrName>attributeName</attrName>
</param>

这显然是错误的.当然,我不是世界上唯一遇到此问题的人吗?自2011年以来,关于少量功能的文档是否发生了变化?

which is clearly wrong. Surely I'm not the only person in the world to have this problem? Did the documentation on that little bit of functionality change since 2011?

推荐答案

是的,您不是唯一遇到此问题的人-我看到无数的帖子,人们声称使用SOAP_ENC_OBJECT传递给SoapVar的数组可以解决此问题.而其他人则在相同的职位上主张其他权利.而且原因尚不清楚(除了我一年前在php.net的评论部分发表的文章).

Yes, you are not the only one who having this issue - I have seen countless number of posts where people claim that array passed to SoapVar with SOAP_ENC_OBJECT solves the issue while other people claim otherwise on the same posts. And documentation is not clear on the reasons (apart from my post year ago in comments section of php.net).

面对同样的问题,我已经阅读了PHP SOAP扩展的来源.基本上,您使用的语法绝对正确:

Facing the same issue I have read the sources of PHP SOAP extension. Basically the syntax you have used is absolutely correct:

$param = array(
    "_" => 'value',
    'attrName' => 'attributeName'
);
$p = new SoapVar($param, SOAP_ENC_OBJECT);

文档未说明的内容:此语法可能产生两种不同的结果(实际上甚至更多:PHP SOAP可以用八种不同的方式表示它).正如您可能会看到的那样,上面的代码是模棱两可的:上面的代码中所说的'attrName'是属性而不是元素?没有.上面的代码只是没有足够的信息量供SoapClient决定"attrName"是什么,因此默认为元素".

What the documentation does not say: this syntax may produce two different results (and in fact even more: PHP SOAP may express it in eight different ways). And as you may see the code above is ambiguous: what in the code above says that 'attrName' is an attribute and not an element? Nothing. The code above just don't have sufficient amount of information for SoapClient to decide what 'attrName' is and so it defaults to "an element".

SoapClient可以在两种模式下运行:非WSDL和WSDL.在以前的模式下,您将永远无法获得所需的结果:SoapClient依赖于类型信息才能将数组元素转换为属性.由于在非WSDL模式下不存在类型信息,因此SoapClient将提供的数组表示为元素集-正是您所得到的.在WSDL模式下,存在类型信息,因此SoapClient知道元素和属性名称,并可以将它们与数组索引匹配.因此,如果需要属性,则必须使SoapClient处于WSDL模式.

SoapClient may operate in two modes: non-WSDL and WSDL. In former mode you would never get the result you want: SoapClient relies on type information in order to turn array element into attribute. As type information is not present in non-WSDL mode SoapClient represents the provided array as set of elements - exactly what you got. In WSDL mode type information is present and therefore SoapClient knows elements and attributes names and may match them to array indexes. So you MUST have your SoapClient in WSDL mode if you want your attributes.

基本上,为了实现所需的功能,您需要在< xsd的相应部分中使用< xsd:attribute name ="attrName" type ="xsd:string"/>在文档/文字模式下使用WSDL文件: schema>块.

Basically in order to achieve what you want you need to have WSDL file in document/literal mode with <xsd:attribute name="attrName" type="xsd:string"/> in appropriate section in <xsd:schema> block.

有些人声称成功使用属性数组而有些人说它不起作用"的原因仅在于他们的设置:有些人要使用WSDL文件,有些人只是尝试做新的SoapClient(null,大批(...)); (这当然没有达到他们的期望)

The reason why some people claim success about array use for attributes and other people say "It does not work" lies solely in their setup: some people have WSDL files to consume, some people just trying to do new SoapClient(null, array(...)); (which of course fails their expectations)

这篇关于PHP的SoapVar没有设置属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆