C# Friendly Yii Web service

I am attempting to write a Yii web service that takes no parameters and returns an array of objects.

The web service will be consumed from .net so I would like to be able to write a simple test .net application to demonstrate that it works.

Currently I have a Controller for my web service and a Class for the array of objects. By default Yii seems to return the array of objects like print_r would return it instead of an XML document with a soap envelope like C# seems to expect based on the wsdl.

Am I supposed to create the soap envelope and XML from scratch? I have looked at the phonebook example and it seems to respond with the same type of text encoded array of objects as my application does instead of soap envelope xml.

I cannot seem to find anything in the documentation relating to what I should expect as a response from yii web services but it does not seem to be XML based on the examples I have looked at.

A Controller For my web services




<?php

class ServicesController extends Controller {


    public function actions() {

        return array(

            'branches' => array(

                'class' => 'CWebServiceAction',

                'classMap'=>array(

                    'Branchlisting'=>'Branchlisting'

                ),

            ),

        );

    }


    /**

     * @return Branchlisting[]

     * @soap

     */    

    public function GetAllBranches() {

        $branches = Branch::model()->findAll();

        foreach ($branches as $branch) {

            $address = "";

            if ($branch->address_line1 != "") {

                $address = $branch->address_line1;

            }

            if ($branch->address_line2 != "") {

                if ($address != "") {

                    $address .= ", ";

                }

                $address .= $branch->address_line2;

            }

            if ($branch->address_line3 != "") {

                if ($address != "") {

                    $address .= ", ";

                }

                $address .= $branch->address_line3;

            }

            if ($branch->address_line4 != "") {

                if ($address != "") {

                    $address .= ", ";

                }

                $address .= $branch->address_line4;

            }

            $branchHoursSunday = Branchhour::model()->find("branchId = '{$branch->id}' and day_number = 0");

            $branchHoursSaturday = Branchhour::model()->find("branchId = '{$branch->id}' and  day_number = 6");

            $branchHoursWeekday  = Branchhour::model()->find("branchId = '{$branch->id}' and  day_number = 1");

            $branchHoursPublicHoliday = Branchhour::model()->find("branchId = '{$branch->id}' and  day_number = 7");


            $WeekDayHours = "";

            $branchListings[] = new Branchlisting (

                $branch->name,

                $address,

                $branch->telephone,

                $branch->fax,

                $branch->region->short_code,

                $branch->code,

                $branch->contact_person_name,

                $branchHoursWeekday,

                $branchHoursSaturday,

                $branchHoursSunday,

                ($branch->is_airport == 1 ? "true" : "false"),

                $branchHoursPublicHoliday

            );

        }

        

        return $branchListings;

    }


    

    

    public function actionTestservice()

    {

        $client=new SoapClient(<webservice url>);


        $result = $client->GetAllBranches();

        

        var_dump($result);

    }

}

A Class for the object array




<?php

class Branchlisting

{    

    /**

     * @var string BranchName

     * @soap

     */

    public $BranchName;

    /**

     * @var string Address

     * @soap

     */    

    public $Address;

    /**

     * @var string TelNo

     * @soap

     */

    public $TelNo;

    /**

     * @var string FaxNo

     * @soap

     */

    public $FaxNo;

    /**

     * @var string Province

     * @soap

     */

    public $Province;

    /**

     * @var string xCode

     * @soap

     */

    public $xCode;

    /**

     * @var string Contact

     * @soap

     */

    public $Contact;

    /**

     * @var string WeekDayHours

     * @soap

     */

    public $WeekDayHours;

    /**

     * @var string SaturdayHours

     * @soap

     */

    public $SaturdayHours;

    /**

     * @var string SundayHours

     * @soap

     */

    public $SundayHours;

    /**

     * @var string IsAirport

     * @soap

     */

    public $IsAirport;

    /**

     * @var string PublicHolidayHours

     * @soap

     */

    public $PublicHolidayHours;

    

    public function __construct($aBranchName=null, $aAddress=null, $aTelNo=null, $aFaxNo=null, $aProvince=null, $axCode=null, $aContact=null, $aWeekDayHours=null, $aSaturdayHours=null, $aSundayHours=null, $aIsAirport=null, $aPublicHolidayHours=null) 

    {

        $this->BranchName = $aBranchName;

        $this->Address = $aAddress;

        $this->TelNo = $aTelNo;

        $this->FaxNo = $aFaxNo;

        $this->Province = $aProvince;

        $this->xCode = $axCode;

        $this->Contact = $aContact;

        $this->WeekDayHours = self::branchHoursToString($aWeekDayHours);

        $this->SaturdayHours = self::branchHoursToString($aSaturdayHours);

        $this->SundayHours = self::branchHoursToString($aSundayHours);

        $this->IsAirport = $aIsAirport;

        $this->PublicHolidayHours = self::branchHoursToString($aPublicHolidayHours);

    }

    

    private static function branchHoursToString($oBranchHours) {

        if (is_object($oBranchHours)) {

            if ($oBranchHours->open_time == "00:00:00" && $oBranchHours->close_time == "00:00:00") {

                $hoursstring = "-";                

            } else {

                $hoursstring = substr($oBranchHours->open_time,0,5);

                if ((int)substr($oBranchHours->open_time, 0, 2) < 12) {

                    $hoursstring .= " AM";

                } else {

                    $hoursstring .= " PM";

                }

                $hoursstring .= " - ";

                $hoursstring .= substr($oBranchHours->close_time,0,5);

                if ((int)substr($oBranchHours->close_time, 0, 2) < 12) {

                    $hoursstring .= " AM";

                } else {

                    $hoursstring .= " PM";

                }

            }

        }

        return $hoursstring;

    }

}

I was very confused when I wrote this post and has since solved my problem, I now have a Yii web service and a .net test client that works great.

A Soap envelope is surrounding the messages sent between Soap server and Soap client.

Years ago, for a quick test I adapted a system generated stub. Thus, not all attributes may be mandatory




using System;


[System.Web.Services.WebServiceBinding(Name="ItemControllerBinding", Namespace="urn:ItemControllerwsdl")]

public partial class itemsWS : System.Web.Services.Protocols.SoapHttpClientProtocol

{

    public itemsWS()

    {

      this.Url = "http://mysite/myapp/index.php?r=item/itemsWS&ws=1";

    }


[System.Web.Services.Protocols.SoapRpcMethodAttribute("urn:ItemControllerwsdl#getFirstItem",

RequestNamespace="urn:ItemControllerwsdl",

ResponseNamespace="urn:ItemControllerwsdl",

Use=System.Web.Services.Description.SoapBindingUse.Encoded)]

    public string getFirstItem() 

    {

        object[] results = this.Invoke("getFirstItem", new object[0]);

        return ((string)(results[0]));

    }

    //...

}



/Tommy

Edit:

Glad you solved it

Edit2:

The call




public class test

{

  static void Main()

  {

    itemsWS ws = new itemsWS();

    string s = ws.getFirstItem();

  }

  /...

}



(I didn’t have Visual Studio installed when performing this test)