抱歉我的英文。我正在尝试在Yii2上重建我的旧肥皂服务器。
我无法格式化与旧服务响应相同的元素名称。
我该怎么做?
旧的服务响应是
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://api.xxx.com">
<SOAP-ENV:Body>
<ns1:GetAllColorsResponse>
<describedColorArray>
<DescribedColor>
<id>6</id>
<Name>beyaz</Name>
<HTMLColorCode>#000000</HTMLColorCode>
</DescribedColor>
<DescribedColor>
<id>2</id>
<Name>kırmızı</Name>
<HTMLColorCode>#FF0000</HTMLColorCode>
</DescribedColor>
<DescribedColor>
<id>4</id>
<Name>mavi</Name>
<HTMLColorCode>#0000FF</HTMLColorCode>
</DescribedColor>
<DescribedColor>
<id>1</id>
<Name>sarı</Name>
<HTMLColorCode>#909000</HTMLColorCode>
</DescribedColor>
<DescribedColor>
<id>3</id>
<Name>turuncu</Name>
<HTMLColorCode>#FF9900</HTMLColorCode>
</DescribedColor>
<DescribedColor>
<id>5</id>
<Name>yeşil</Name>
<HTMLColorCode>#009000</HTMLColorCode>
</DescribedColor>
</describedColorArray>
</ns1:GetAllColorsResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Yii2肥皂服务响应
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://newapi.xxx.com/api/GetAllColors?ws=1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns2="http://xml.apache.org/xml-soap" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
<ns1:GetAllColorsResponse>
<return xsi:type="ns2:Map">
<item>
<key xsi:type="xsd:string">describedColorArray</key>
<value xsi:type="ns2:Map">
<item>
<key xsi:type="xsd:string">DescribedColor</key>
<value SOAP-ENC:arrayType="ns2:Map[6]" xsi:type="SOAP-ENC:Array">
<item xsi:type="ns2:Map">
<item>
<key xsi:type="xsd:string">id</key>
<value xsi:type="xsd:int">6</value>
</item>
<item>
<key xsi:type="xsd:string">name</key>
<value xsi:type="xsd:string">beyaz</value>
</item>
</item>
<item xsi:type="ns2:Map">
<item>
<key xsi:type="xsd:string">id</key>
<value xsi:type="xsd:int">2</value>
</item>
<item>
<key xsi:type="xsd:string">name</key>
<value xsi:type="xsd:string">kırmızı</value>
</item>
</item>
<item xsi:type="ns2:Map">
<item>
<key xsi:type="xsd:string">id</key>
<value xsi:type="xsd:int">4</value>
</item>
<item>
<key xsi:type="xsd:string">name</key>
<value xsi:type="xsd:string">mavi</value>
</item>
</item>
<item xsi:type="ns2:Map">
<item>
<key xsi:type="xsd:string">id</key>
<value xsi:type="xsd:int">1</value>
</item>
<item>
<key xsi:type="xsd:string">name</key>
<value xsi:type="xsd:string">sarı</value>
</item>
</item>
<item xsi:type="ns2:Map">
<item>
<key xsi:type="xsd:string">id</key>
<value xsi:type="xsd:int">3</value>
</item>
<item>
<key xsi:type="xsd:string">name</key>
<value xsi:type="xsd:string">turuncu</value>
</item>
</item>
<item xsi:type="ns2:Map">
<item>
<key xsi:type="xsd:string">id</key>
<value xsi:type="xsd:int">5</value>
</item>
<item>
<key xsi:type="xsd:string">name</key>
<value xsi:type="xsd:string">yeşil</value>
</item>
</item>
</value>
</item>
</value>
</item>
</return>
</ns1:GetAllColorsResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Yii2控制器代码
public function actions()
{
return [
'GetAllColors' => [
'class' => 'mongosoft\soapserver\Action',
'serviceOptions' => [
'disableWsdlMode' => true,
]
]
];
}
/**
* @return array
* @soap
*/
public function GetAllColors()
{
$model = GeneralColor::find()->orderBy('name')->asArray()->all();
foreach($model as $m){
$row['id'] = $m['id'];
$row['name'] = $m['name'];
$rows['describedColorArray']['DescribedColor'][] = $row;
}
return $rows;
}
答案 0 :(得分:1)
尝试使用PHP类,并使用“classMap”属性将它们定义为复杂类型。设置所需的SOAP版本没有坏处(默认为“null”)。
例如:
public function actions()
{
return [
'GetAllColors' => [
'class' => 'mongosoft\soapserver\Action',
'serviceOptions' => [
'disableWsdlMode' => true,
'soapVersion' => '1.1'
],
'classMap' => [
'DescribedColor' => 'GeneralColor'
]
]
];
}
public function GetAllColors()
{
$model = GeneralColor::find()->orderBy('name')->asArray()->all();
return array("describedColorArray" => $model);
}
答案 1 :(得分:0)
尝试@return object
而非@return array
并添加一些代码
$std = new \stdClass();
return $std
答案 2 :(得分:-1)
public function actions()
{
return [
'GetAllColors' => [
'class' => 'mongosoft\soapserver\Action',
'serviceOptions' => [
'disableWsdlMode' => true, 'soapVersion' => '1.1',
]
]
];
}