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?.
PHPWeb Services

Avatar of undefined
Last Comment
Rose Babu

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Rose Babu

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
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?
Rose Babu

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

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy