簡體   English   中英

PHP Soap Client:如何使用Derived類作為參數調用WebService?

[英]PHP Soap Client: How call WebService with Derived class as parameter?

我正在使用PHP 5,並且想調用定義如下的Web服務:

webmethod ( AbstractBase obj );

我正在使用SoapClient (基於wsdl)。 Web方法需要AbstractBase的子類 但是,在PHP中,調用soap方法會使我遇到此錯誤:

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

我很確定問題是我必須在Soap調用中指定obj參數的類型-但我似乎找不到使它如此的魔術字。

    $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個類,並嘗試使用SoapVar和XSD_ANYTYPE顯式設置對象的類型。 幾乎可以正常工作-但它沒有在類的字段中設置名稱空間(ns1 :)。

所以我最終如何解決這個問題? 花了兩件事。

我發現了很棒的XSD_ANYXML 這使我可以為請求滾動自己的XML。 它本身無法將xsi名稱空間添加到soap信封。 因此,我必須強制一個參數為XSD_STRING才能喚醒正在構建請求的代碼。 我的工作代碼是:

$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);

我想知道您正在傳遞的數組(而不是對象)是否被投射到某個地方的AbstractBase中,然后引發了錯誤。 您可能需要傳遞一個實際對象:

abstract class AbstractBase {
    public $internal_id;
    public $external_id;
}

class DerivedClass extends AbstractBase { }

$obj = new DerivedClass();
$obj->internal_id = $internalId;
$obj->external_id = $externalId;

$params = array(
   'obj'      => $obj  // Now an instance of DerivedClass
);

$response = $client->webmethod($params);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM