使用PHP返回Dictionary对象&;肥皂
我正在用PHP编写一个SOAP API,我被卡住了。我正在尝试返回以下格式的Dictionary对象:使用PHP返回Dictionary对象&;肥皂,php,soap,dictionary,xsd,wsdl,Php,Soap,Dictionary,Xsd,Wsdl,我正在用PHP编写一个SOAP API,我被卡住了。我正在尝试返回以下格式的Dictionary对象: Key: # Value: Id: # Title: some title Text: blah 我一直在浏览另一个站点的soapapi服务的WSDL和XSD文件,我已经找到了如何在其中写入Dictionary对象,现在我被困在PHP部分。我的字典是在WSDL/XSD中设置的,以整数作为键,以注释对象作为值,因此它应该是一个int键/注释值对数组……但我不知道如何在P
Key: #
Value:
Id: #
Title: some title
Text: blah
我一直在浏览另一个站点的soapapi服务的WSDL和XSD文件,我已经找到了如何在其中写入Dictionary对象,现在我被困在PHP部分。我的字典是在WSDL/XSD中设置的,以整数作为键,以注释对象作为值,因此它应该是一个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和名称、文本、日期等。我希望Dictionary键是Comment id,值是Comment对象本身
有人能帮我解决这个问题吗?帮我想一想在PHP中应该如何工作。谢谢 好吧,既然我没有更好的事可做,我就一直在修修补补,想办法解决它。我有大多数正确的元素,只是顺序不对 我的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”的字符串,我只是把它作为测试放在那里。它还包含一个无限的ArrayOfKeyValueofInComment对象,这就是神奇发生的地方。最后,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;
}
class GetDictionaryResponse{
var$GetDictionaryResult;
函数GetDictionaryResponse(){
$this->GetDictionaryResult=new GetDictionaryResult();
}
}
类GetDictionaryResult{
var$状态;
var$数据;
函数GetDictionaryResult(){
$this->status=(字符串)“Ok”;
}
函数附加项($k,$v){
$d=新阵列FKEYVALUE OFINTCOMMENT($k,$v);
$this->Data[]=$d;
}
}
类ArrayOfKeyValueOfInComment{
var$Key$Value;
函数ArrayOfKeyValueOfInComment($k,$v){
$this->Key=$k;
$this->Value=$v;
}
}
函数GetDictionary(){
$ret=新的GetDictionaryResponse();
对于($i=0;$i<3;$i++){
$c=新注释(数组([Comment elements]);
$ret->GetDictionaryResult->AddItem($i,$c);
}
返回$ret;
}
希望它看起来不会太混乱。基本上,返回如下所示:
GetDictionaryResponse->
GetDictionaryResult->
string status;
Dictionary<int, Comment> Data;
GetDictionaryResponse->
GetDictionaryResult->
字符串状态;
词典数据;
我可以在.Net中使用它并从中获得一个有效的字典,如下所示:
GetDictionaryResponse->
GetDictionaryResult->
string status;
Dictionary<int, Comment> Data;
DictionaryResponse r=client.GetDictionary();
注释c=r.数据[0]
希望我已经帮助了一些未来的读者