Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format Yii2 Soap Server Response same as my Old Php Soap Service

Tags:

php

soap

yii2

Sorry about my English. I am trying to rebuild my old soap server on Yii2.

I couldn't format the element names same as old service response.

How can i do it?

The old service response is

   <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 service response is

<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 Controller Code is

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;
}
like image 826
Burhan Çetin Avatar asked Jul 07 '15 16:07

Burhan Çetin


1 Answers

Try to use PHP classes and define these as complex types using the "classMap" attribute. No harm setting the required SOAP version (the default is "null").

For example:

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);
}
like image 130
daz Avatar answered Sep 30 '22 11:09

daz