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 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染