使用 PHP & 返回一个 Dictionary 对象肥皂

发布于 2024-11-29 14:42:59 字数 1213 浏览 0 评论 0原文

我正在用 PHP 编写 SOAP API,但我陷入了困境。我正在尝试返回具有以下格式的 Dictionary 对象:

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

我一直在查看另一个站点的 SOAP API 服务的 WSDL 和 XSD 文件,并且已经弄清楚如何将 Dictionary 对象写入其中,现在我陷入了困境在PHP部分。我的字典在 WSDL / XSD 中设置为以整数作为键,以 Comment 对象作为值,因此它应该是 int 键/注释值对的数组......但我只是不知道如何用 PHP 来做。这是我在 XSD 文件中得到的结构:

  <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 和名称、文本、日期等。我想要字典键是 Comment id,值是 Comment 对象本身。

有人可以帮我解决这个问题吗?帮我想想这在 PHP 中应该如何工作。谢谢!

I'm writing a SOAP API in PHP and I'm stuck. I'm trying to return a Dictionary object with this format:

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

I've been looking through the WSDL and XSD files of another site's SOAP API service and I've figured out how to write a Dictionary object into them, now I'm stuck at the PHP part. My Dictionary is set up in the WSDL / XSD to have an integer as the key and a Comment object as the value, so it should be an array of int key / Comment value pairs...but I just can't figure out how to do it in PHP. Here is the structure I've got in my XSD file:

  <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>

The Comment object just contains a few pieces of information, like an id, the poster id and name, text, date, etc. I want the Dictionary key to be the Comment id, and the value to be the Comment object itself.

Can someone help me out with this, just help me wrap my head around how this should work in PHP. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

治碍 2024-12-06 14:42:59

好吧,因为我没有更好的事情可做,所以我一直在修修补补并想出办法。我拥有大部分正确的元素,只是顺序错误。

我的 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 对象中。我的第二个 XSD 是我出错的地方,SOAP 令人困惑,但我最终得到了这些元素:

<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 对象包含一个名为“status”的字符串,我只是将其放在那里作为测试。它还包含一个无界的 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;

我可以在.Net中使用它并从中获取有效的字典,如下所示:

DictionaryResponse r = client.GetDictionary();
注释 c = r.Data[0];

希望我能帮助一些未来的读者!

Well, since I had nothing better to do, I kept tinkering with it and figured it out. I had most of the right elements, just in the wrong order.

My WSDL stayed the same, I didn't have to change anything there. In my first XSD, I changed my output element to this:

<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>

Basically I'm returning a DictionaryResponse object named GetDictionaryResult from the second XSD, the DictionaryResponse is wrapped in a GetDictionaryResponse object. My second XSD was where I was going wrong, SOAP is confusing, but I ended up with these elements:

<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" />

The DictionaryResponse object contains a string called "status", which I only put there as a test. It also contains an unbounded ArrayOfKeyValueOfintComment objects, which is where the magic happens. Finally, the PHP itself:

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;
  }

Hopefully it doesn't look too confusing. Essentially the return looks like this:

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

I can consume it in .Net and get a valid Dictionary from it like this:

DictionaryResponse r = client.GetDictionary();
Comment c = r.Data[0];

Hopefully I've helped some future readers out!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文