簡體   English   中英

使用PHP SoapClient類映射選項,其中WSDL包含具有相同名稱的元素和complexType

[英]Using PHP SoapClient classmap option with WSDL containing an element and complexType with the same name

我遇到了一些不同的WSDL文件,其中包含一個元素和一個具有相同名稱的complexType。 例如, http ://soap.search.msn.com/webservices.asmx?wsdl有兩個名為“SearchResponse”的實體:

在這種情況下,我無法弄清楚如何使用SoapClient()“classmaps”選項將這些實體正確映射到PHP類。

PHP手冊說:

classmap選項可用於將某些WSDL類型映射到PHP類。 此選項必須是一個數組,其中WSDL類型作為鍵,PHP類的名稱作為值。

不幸的是,由於有兩個具有相同密鑰的WSDL類型('SearchResponse'),我無法弄清楚如何區分兩個SearchResponse實體並將它們分配給相應的PHP類。

例如,以下是示例WSDL的相關代碼段:

<xsd:complexType name="SearchResponse">
    <xsd:sequence>
        <xsd:element minOccurs="1" maxOccurs="1" name="Responses" type="tns:ArrayOfSourceResponseResponses"/>
    </xsd:sequence>
</xsd:complexType>

<xsd:element name="SearchResponse">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element minOccurs="1" maxOccurs="1" name="Response" type="tns:SearchResponse"/>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>

這里的PHP顯然不起作用,因為類映射鍵是相同的:

<?php $server = new SoapClient("http://soap.search.msn.com/webservices.asmx?wsdl", array('classmap' => array('SearchResponse' => 'MySearchResponseElement', 'SearchResponse' => 'MySearchResponseComplexType'))); ?>

在搜索解決方案時,我發現Java Web Services通過允許您為“Element”或“ComplexType”實體指定自定義后綴來處理此問題。

http://download.oracle.com/docs/cd/E17802_01/webservices/webservices/docs/1.5/tutorial/doc/JAXBUsing4.html#wp149350

所以,現在我覺得沒有辦法用PHP的SoapClient做到這一點,但我很好奇是否有人可以提供任何建議。 FWIW,我無法編輯遠程WDSL。

有任何想法嗎???

這是我的頭腦,但我認為你可以讓兩個SearchResponse類型映射到MY_SearchResponse並嘗試抽象兩者之間的差異。 它是kludgy,但這樣的事可能嗎?

<?php
//Assuming SearchResponse<complexType> contains SearchReponse<element> which contains and Array of SourceResponses
//You could try abstracting the nested Hierarchy like so:
class MY_SearchResponse
{
   protected $Responses;
   protected $Response;

   /**
    * This should return the nested SearchReponse<element> as a MY_SearchRepsonse or NULL
    **/
   public function get_search_response()
   {
      if($this->Response && isset($this->Response))
      {
         return $this->Response; //This should also be a MY_SearchResponse
      }
      return NULL;
   }

   /**
    * This should return an array of SourceList Responses or NULL
    **/
   public function get_source_responses()
   {
      //If this is an instance of the top SearchResponse<complexType>, try to get the SearchResponse<element> and it's source responses
      if($this->get_search_response() && isset($this->get_search_response()->get_source_responses()))
      {
         return $this->get_search_response()->get_source_responses();
      }
      //We are already the nested SearchReponse<element> just go directly at the Responses
      elseif($this->Responses && is_array($this->Responses)
      {
         return $this->Responses;
      }
      return NULL;
   }
}

class MY_SourceResponse
{
  //whatever properties SourceResponses have
}

$client = new SoapClient("http:/theurl.asmx?wsdl", array('classmap' => array('SearchResponse' => 'MY_SearchResponse', 'SourceResponse' => 'MY_SourceResponse')));

暫無
暫無

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

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