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

abie-hadi
abie-hadi used Ask the Experts™
on
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?.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Team Manager
Commented:
can you try print_r($guys) ? check what displayed in webpage as well as in viewsource.

and instead returning the dataset, why dont you retun the array of values and parse the data in php?
if you return the array of values like below, then it will be very easy to parse in php.

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetGuysDataResponse xmlns="http://tempuri.org/">
      <GetGuysDataResult>
        <GuysDataResponse>
          <id>string</id>
          <name>string</name>
          <email>string</email>
        </GuysDataResponse>
        <GuysDataResponse>
          <id>string</id>
          <name>string</name>
          <email>string</email>
        </GuysDataResponse>
      </GetGuysDataResult>
    </GetGuysDataResponse>
  </soap:Body>
</soap:Envelope>

Open in new window

Author

Commented:
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 BabuSenior Team Manager

Commented:
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial