2011-08-15 8 views
0

私はPHPでSOAP APIを書いていますが、私は立ち往生しています。私はこのフォーマットでDictionaryオブジェクトを返すようにしようとしている:PHPとSOAPで辞書オブジェクトを返す

Key: # 
Value: 
    Id: # 
    Title: some title 
    Text: blah 

私は別のサイトのSOAP APIサービスのWSDLおよびXSDファイルを見てきたと私は辞書オブジェクトを作成する方法を考え出しましたそれらは、今私はPHPの部分で立ち往生しています。私のディクショナリはWSDL/XSDの中でキーとして整数を、値としてCommentオブジェクトを持つように設定されているので、intキー/コメント値のペアの配列でなければなりません...しかし、 PHPでそれを行う。

<xs:complexType name="DictionaryResponse"> 
    <xs:annotation> 
     <xs:appinfo> 
     <IsDictionary xmlns="http://schemas.microsoft.com/2003/10/Serialization/">true</IsDictionary> 
     </xs:appinfo> 
    </xs:annotation> 
    <xs:sequence> 
     <xs:element minOccurs="0" maxOccurs="unbounded" name="DictionaryResult"> 
     <xs:complexType> 
      <xs:sequence> 
      <xs:element name="Key" type="xs:int" /> 
      <xs:element name="Value" nillable="true" type="tns:Comment" /> 
      </xs:sequence> 
     </xs:complexType> 
     </xs:element> 
    </xs:sequence> 

Commentオブジェクトはただのidのように、情報のいくつかの作品が含まれている、ポスターのIDと名前、テキスト、日付、など私がしたい:ここで私は私のXSDファイル内に持っている構造でありますCommentキーになるDictionaryキー、Commentオブジェクト自体の値。

誰かがこれで私を助けてくれますか?PHPでどのように動作するかを頭に入れてください。ありがとう!

答えて

0

私は何もしていないので、私はそれを試してみて、それを理解しました。私は間違った順序でほとんどの正しい要素を持っていました。

私のWSDLは変わっていませんでしたが、変更する必要はありませんでした。私の最初のXSDでは、私はこれに私の出力要素を変更:

<xs:element name="GetDictionaryResponse"> 
    <xs:complexType> 
    <xs:sequence> 
     <xs:element minOccurs="0" name="GetDictionaryResult" nillable="true" type="q2:DictionaryResponse" xmlns:q2="http://reachchallenges.infectionist.com/api/" /> 
    </xs:sequence> 
    </xs:complexType> 
</xs:element> 

基本的に私は、第二のXSDからGetDictionaryResultという名前DictionaryResponseオブジェクトを返すよ、DictionaryResponseはGetDictionaryResponseオブジェクトに包まれています。

<xs:complexType name="DictionaryResponse"> 
    <xs:sequence> 
    <xs:element minOccurs="0" name="status" nillable="true" type="xs:string" /> 
    <xs:element minOccurs="0" name="Data" type="tns:ArrayOfKeyValueOfintComment" /> 
    </xs:sequence> 
</xs:complexType> 
<xs:element name="DictionaryResponse" nillable="true" type="tns:DictionaryResponse" /> 

<xs:complexType name="ArrayOfKeyValueOfintComment"> 
    <xs:annotation> 
    <xs:appinfo> 
     <IsDictionary xmlns="http://schemas.microsoft.com/2003/10/Serialization/">true</IsDictionary> 
    </xs:appinfo> 
    </xs:annotation> 
    <xs:sequence> 
    <xs:element minOccurs="0" maxOccurs="unbounded" name="ArrayOfKeyValueOfintComment"> 
     <xs:complexType> 
     <xs:sequence> 
      <xs:element name="Key" type="xs:int" /> 
      <xs:element name="Value" nillable="true" type="tns:Comment" /> 
     </xs:sequence> 
     </xs:complexType> 
    </xs:element> 
    </xs:sequence> 
</xs:complexType> 
<xs:element name="ArrayOfKeyValueOfintComment" nillable="true" type="tns:ArrayOfKeyValueOfintComment" /> 

DictionaryResponseオブジェクトは、私が唯一のテストとしてそこに置く「ステータス」と呼ばれる文字列を、含まれています。私の第二XSDは、私が間違って行っていたところ、SOAPは紛らわしいですが、私は、これらの要素になってしまったでした。また、無限のArrayOfKeyValueOfintCommentオブジェクトも含まれています。これは、マジックが起こる場所です。最後に、PHP自体:

class GetDictionaryResponse { 
    var $GetDictionaryResult; 

    function GetDictionaryResponse() { 
    $this->GetDictionaryResult = new GetDictionaryResult(); 
    } 
    } 

class GetDictionaryResult { 
    var $status; 
    var $Data; 

    function GetDictionaryResult() { 
    $this->status = (string)"Ok"; 
    } 

    function AddItem($k, $v) { 
    $d = new ArrayOfKeyValueOfintComment($k, $v); 
    $this->Data[] = $d; 
    } 
    } 

class ArrayOfKeyValueOfintComment { 
    var $Key, $Value; 

    function ArrayOfKeyValueOfintComment($k, $v) { 
    $this->Key = $k; 
    $this->Value = $v; 
    } 
    } 

function GetDictionary() { 
    $ret = new GetDictionaryResponse(); 
    for($i = 0; $i < 3; $i++) { 
    $c = new Comment(array([comment elements])); 
    $ret->GetDictionaryResult->AddItem($i, $c); 
    } 

    return $ret; 
    } 

うまくいけばあまり混乱しないように見えます。基本的にリターンは次のようになります。

GetDictionaryResponse-> 
    GetDictionaryResult-> 
    string status; 
    Dictionary<int, Comment> Data; 

私はネットでそれを消費し、このようにそれから有効な辞書を取得することができます。

DictionaryResponse R = client.GetDictionary(); コメントc = r.Data [0];

今後の読者を支援してくれたらと思っています。

関連する問題