Link to home
Start Free TrialLog in
Avatar of abie-hadi
abie-hadi

asked on

How to consume an asp.net webservice with complex types from php?

Dear Experts

This is part of my webmethod in c#:




client.Open();

            SqlCommand query = new SqlCommand();

            query.CommandText = "SELECT id,name,email FROM guys";
            query.Connection = client;
           
           

            DataSet guysdata = new DataSet("guys");

            SqlDataAdapter readadata = new SqlDataAdapter();

            readdata.SelectCommand = query;
            readdata.Fill(guysdata);

            client.Close();

            return guysdata;

I tried to consume it from php with no luck:

$service = new SoapClient('http://localhost/service.asmx?WSDL');

$guys= $service->guys()->guysResult;

If I try to echo $guys I'll get a "Object of class stdClass could not be converted to string". Would you please tell me how to deal with datasets coming from asp.net webservices in PHP?.
ASKER CERTIFIED SOLUTION
Avatar of Rose Babu
Rose Babu
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of abie-hadi
abie-hadi

ASKER

Thanks! , The Print_r command works

I don't know how to pass the data as an array in c# and parse using php, Give me a hint please?
Here is the sample web service for you

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebService_CSHARP
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://www.yournamespace.com/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
		// Response Class Structure
        public class GuysDataResponse
        {
            public string id;
            public string name;
            public string email;

        }

        [WebMethod]
        public GuysDataResponse[] GetGuysData()
        {

	    // Update your code here and fill the guysdata dataset
            //client.Open();

            //SqlCommand query = new SqlCommand();

            //query.CommandText = "SELECT id,name,email FROM guys";
            //query.Connection = client;
            
            //DataSet guysdata = new DataSet("guys");

            //SqlDataAdapter readadata = new SqlDataAdapter();

            //readdata.SelectCommand = query;
            //readdata.Fill(guysdata);

            //client.Close();

            DataTable  dt = new DataTable();
            DataRow dr;
            dt = guysdata.Tables[0];
            
            GuysDataResponse[] gData = new GuysDataResponse[dt.Rows.Count];

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dr = dt.Rows[i];
                gData[i] = new GuysDataResponse();

                gData[i].id = dr["id"].ToString();
                gData[i].name = dr["name"].ToString();
                gData[i].email = dr["email"].ToString();

            }

            return gData;

        }

    }
}

Open in new window