dongtan7201 2018-05-14 18:14
浏览 149
已采纳

PHP SOAP返回具有属性的相同名称元素的数组

I am trying to return values the way they are sent into my soap server so they have the same XML structure.

Here is a sample request for multiple policies with various attributes.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdl="https://customer-soap.example.com/?WSDL">
   <soapenv:Header/>
   <soapenv:Body>
      <wsdl:SendPoliciesRequestInput>
         <sesid>someid</sesid>
         <!--Optional:-->
         <policy product="product1" company="ges1" polizzenNr="pol1" premium="1" expiry="01.01.2000" info="blabla1"/>
         <policy product="product2" company="ges2" polizzenNr="pol2" premium="2" expiry="02.01.2000" info="blabla2"/>
      </wsdl:SendPoliciesRequestInput>
   </soapenv:Body>
</soapenv:Envelope>

In my php code this translates to an object containing a sesid and an array of policy elements like this: (print_r($request))

stdClass Object
(
    [sesid] => someid
    [policy] => Array
        (
            [0] => stdClass Object
                (
                    [product] => product1
                    [company] => ges1
                    [Nr] => pol1
                    [premium] => 1
                    [expiry] => 01.01.2000
                    [info] => blabla1
                )

            [1] => stdClass Object
                (
                    [product] => product2
                    [company] => ges2
                    [Nr] => pol2
                    [premium] => 2
                    [expiry] => 02.01.2000
                    [info] => blabla2
                )

When trying to return this exact object back to the caller the following code

<php
class Example
{
  public function SendPolicies($request)
  {
    return $request;
  }
}

outputs like this:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdl="https://customer-soap.example.com/?WSDL">
       <soapenv:Header/>
       <soapenv:Body>
          <wsdl:SendPoliciesRequestOutput>
             <sesid>someid</sesid>
             <!--Optional:-->
             <policy />
          </wsdl:SendPoliciesRequestOutput>
       </soapenv:Body>
    </soapenv:Envelope>

I've tried several approaches including creating SoapVar but this would only restructure the objects instead of creating an output that is exactly the same as the input.

Every help is much appreciated.

  • 写回答

1条回答 默认 最新

  • dongzhi4239 2018-05-15 09:32
    关注

    I ended up using mounties solution: https://stackoverflow.com/a/24894102/8247069

    So not ideal. Another way to do it would be to write the XML yourself:

    $soap->param = new SoapVar("<param attrName='attributeName'>value</param>", XSD_ANYXML);
    

    I don't like either way, but when you've got a deadline, you go with what works.

    The problem seems to be that my code is on the server side, thus (as far as I was able to find out during my research) I am not able to switch into WSDL mode although my server is started using a WSDL.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛