复杂类型的PHP SoapParam / SoapVar给出了“对象没有'xxx'属性” - 重复元素

时间:2011-01-12 09:16:54

标签: php soap repeat complextype

这个问题涉及在PHP SOAP客户端中使用SoapParam和SoapVar来处理重复元素,其中请求不能被构造为关联数组。更具体地说,它解决了在复杂元素中使用SoapParam / SoapVar的难度。

我正在尝试修改工作代码以允许SOAP请求中的重复元素。

工作代码如下,并正确返回单个consignmentID的详细信息。

$oClient = new SoapClient($wsdlFilespec, $arguments);
$parameters = array(
   'header' => array(
      'source' => $_POST['source'],
      'accountNo' => $_POST['accountNo'],
      'userAccessKey' => $connection['userAccessKey']
      ),
   'consignmentId' => $_POST['consignmentId']
     );
$request = array('parameters' => $parameters);
$result = $oClient->__soapCall($operation, $request);

我现在需要能够传入多个consignmentIds,显然关联数组不适用于此。所以我一直在尝试使用SoapParam和SoapVar;顺便说一下,没有为这些找到很多文档或示例。

我尝试了以下内容:

$header = array(
   new SoapParam((string)$_POST['source'], 'source'), 
   new SoapParam((int)$_POST['accountNo'], 'accountNo'),
   new SoapParam((string)$connection['userAccessKey'], 'userAccessKey')
  );

$parameters = array(
   new SoapParam($header, 'header'),
   new SoapParam((string)'PDH44109', 'consignmentId'),
   new SoapParam((string)'PDH44110', 'consignmentId')
     );
$request = array('parameters' => $parameters);

这给出了:SOAP-ERROR:编码:对象没有'header'属性。

我也尝试过使用SoapVar,希望强制复杂类型的“标题”,如下所示:

$header = array(
  new SoapParam((string)$_POST['source'], 'source'), 
  new SoapParam((int)$_POST['accountNo'], 'accountNo'),
  new SoapParam((string)$connection['userAccessKey'], 'userAccessKey')
  );
$headerVar = new SoapVar($header, SOAP_ENC_OBJECT, 'TransactionHeaderType',     
"http://myexpress/Common/actions/externals/Consignment/v1");

$parameters = array(
           new SoapParam($headerVar, 'header'),
   new SoapParam((string)'PDH44109', 'consignmentId'),
   new SoapParam((string)'PDH44110', 'consignmentId')
     );
$request = array('parameters' => $parameters);

这也给出了:SOAP-ERROR:编码:对象没有'header'属性。

我还尝试了最后一行代码的变体,例如:

$request = array('parameters' => $parameters);
$request = array($parameters);
$request = $parameters;

作为一项实验,我暂时将字符串分配给$ header,然后在调用__doRequest之前能够查看__soapCall生成的XML,并发现它包含以下内容:

<SOAPENV:Body><ns1:getConsignmentDetailRequest/>
<consignmentId>PDH44109</consignmentId><consignmentId>PDH44110</consignmentId>
</SOAP-ENV:Body>

您可以看到已正确包含多个货物 - 该部分似乎已解决 - 但完全省略了“标题”(复杂类型)。

非常感谢任何帮助!我是一个真正的初学者,花了一天多的时间。例如,SoapVar非常不确定适当的参数是什么。

“标题”的输入可能存在问题?下面提供了一些wsdl摘录以供参考。

------

<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://my.com.au/ESB/Services/Concrete/External/Services/v1" 

xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns0="http://myexpress/Common/actions/externals/Consignment/v1" 

xmlns:ns1="http://myexpress/Common/externals/Faultv1" xmlns:ns2="http://myexpress/Common/actions/externals/FreightCalculation/v1" 

xmlns:ns3="http://myexpress/Common/Primitives/v1" xmlns:ns4="http://myexpress/Common/FreightProcessing/v1" 

xmlns:ns5="http://myexpress/Common/Account/v1" xmlns:ns6="http://myexpress/Common/Imaging/v1" name="Untitled" 

targetNamespace="http://my.com.au/ESB/Services/Concrete/External/Services/v1">

------

        <xsd:schema xmlns="http://myexpress/Common/Primitives/v1" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 

xmlns:acc="http://myexpress/Common/Account/v1" targetNamespace="http://myexpress/Common/Primitives/v1" elementFormDefault="qualified" 

attributeFormDefault="unqualified">
            <xsd:import namespace="http://myexpress/Common/Account/v1"/>
   .
   .
   .
   .

           <xsd:complexType name="TransactionHeaderType">
                <xsd:sequence>
                    <xsd:element name="source" type="xsd:string"/>
                    <xsd:element name="accountNo" type="xsd:integer"/>
                    <xsd:element name="userAccessKey" type="xsd:string"/>
                    <xsd:element name="userId" type="ns3:userIdType" minOccurs="0"/>
                    <xsd:element name="transactionId" type="ns3:transactionIdType" minOccurs="0"/>
                    <xsd:element name="transactionDatetime" type="xsd:dateTime" minOccurs="0"/>
                </xsd:sequence>
            </xsd:complexType>

------

            <xsd:simpleType name="consignmentIdType">
                <xsd:restriction base="xsd:string">
                    <xsd:maxLength value="30"/>
                </xsd:restriction>
            </xsd:simpleType>

------

            <xsd:element name="getConsignmentDetailRequest">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="header" type="prim:TransactionHeaderType"/>
                        <xsd:element name="consignmentId" type="ns0:consignmentIdType" maxOccurs="unbounded"/>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>

------

1 个答案:

答案 0 :(得分:8)

在与SoapVar和SoapParam争吵几天后无处可寻,找到了以下简单的解决方案:

$oClient = new SoapClient($wsdlFilespec, $arguments); 
$parameters = array(
                    'header' => array(
                                      'source' => $_POST['source'],
                                      'accountNo' => $_POST['accountNo'],
                                      'userAccessKey' => $connection['userAccessKey']
                                     ),
                    'consignmentId' => array('PDH44109', 'PDH44110')
                    );
$request = array('parameters' => $parameters);
$result = $oClient->__soapCall($operation, $request);