doubu1964 2009-06-06 21:50
浏览 53
已采纳

PHP Soap Client:如何使用Derived类调用WebService作为参数?

I'm using PHP 5, and want to call a webservice that is defined sort of like this:

webmethod ( AbstractBase obj );

I'm using a SoapClient (wsdl-based). The web method is expecting a subclass of AbstractBase. Yet, in PHP, calling the soap method gets me this error:

    Server was unable to read request. 
        ---> There is an error in XML document  
        ---> The specified type is abstract: name='AbstractBase'

I'm pretty sure the problem is I have to specify the type of the obj parameter in the Soap call - but I can't seem to find the magic word to make it so.

    $client = new SoapClient($WSDL, $soapSettings);
    $obj = array(
        'internal_id' => $internalId,
        'external_id' => $externald,
    );
    $params = array(
        'obj'      => $obj  // How do I say it is of type: DerivedClass?
    );

    $response = $client->webmethod($params);
  • 写回答

2条回答 默认 最新

  • douruye5092 2009-06-10 12:10
    关注

    That was a good suggestion, but it didn't work either. But it got me going in the right direction. I took your idea, created the 2 classes, and tried to explicitly set the type of the object with a SoapVar and XSD_ANYTYPE. That almost worked - but it did not set the name space (ns1:) on the fields in the class.

    SO how did I eventually fix this? It took 2 things.

    I discovered the wonderful XSD_ANYXML. This lets me roll my own XML for the request. By itself it failed to add the xsi namespace to the soap envelope. So I had to force one parameter to be an XSD_STRING to wake up the code that was building the request. My working code is:

    $client = new SoapClient($WSDL, $soapSettings);
    $myXml = "
      <ns1:obj xsi:type='ns1:DerivedClass'>
        <ns1:internal_id>$internalId</ns1:internal_id>
        <ns1:external_id>$externalId</ns1:external_id>
      </ns1:obj>
    ";
    
    $params = array(
        // this is needed to force the XSI namespace in the header (there must be a better way)
        'foo' => new SoapVar('bar', XSD_STRING, 'String, 'http://www.w3.org/2001/XMLSchema-instance'),
        // this uses the XML I created
        'obj' => new SoapVar($myXml, XSD_ANYXML),
    );
    
    $response = $client->webmethod($params);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置