locked
Return list of class to php via web service. RRS feed

  • Question

  • User-999062906 posted

    Hi,

    I created a c# web service to return a list of classes.

    My php site will call this web service.

    How do I convert the web service result into something that the php can understand?

    I can invoke my c# web service just fine.

    Or is there any better way to do this?

    I don't want to use WCF.

    Thanks.

    Friday, February 28, 2014 4:32 AM

Answers

  • User-999062906 posted

    I'm just trying to tell that my web service is written in c#.

    So here is what I found out:

    my web service return "List<Invoice>".

    the xml looks like this:

    <?xml version="1.0" encoding="utf-8"?>
    <ArrayOfInvoice xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
      <Invoice>
        <_strInvNo>INV001</_strInvNo>
        <_dtDate>2014-03-01T09:51:33.5864815+08:00</_dtDate>
        <_strVehicleNumber>ddd</_strVehicleNumber>
      </Invoice>
      <Invoice>
        <_strInvNo>INV002</_strInvNo>
        <_dtDate>2014-03-01T09:51:33.5864815+08:00</_dtDate>
        <_strVehicleNumber>ddd</_strVehicleNumber>
      </Invoice>
    </ArrayOfInvoice>

    So here is what I did in my php:

    $client = new SoapClient($WebServiceLocation);
    $params = array("strVehicleNumber" => $_SESSION['VehicleNumber']);
    $result = $client->GetOutstandingInvoices($params)->GetOutstandingInvoicesResult;
    //$xml = simplexml_load_string($result->GetOutstandingInvoicesResult);
    echo "<div>".var_dump($result)."</div><br>";
    function obj2array($obj) {
    	$out = array();
    	foreach ($obj as $key => $val) {
    		switch(true) {
    			case is_object($val):
    				$out[$key] = obj2array($val);
    				break;
    			case is_array($val):
    				$out[$key] = obj2array($val);
    				break;
    			default:
    				$out[$key] = $val;
    		}
    	}
    	return $out;
    }
    
    $result = obj2array($result);
    echo "<div>" . var_dump($result) . "</div>";
    echo "<div>" . $result["Invoice"][0]["_strInvNo"] . "</div>";

    Hope this help others.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 28, 2014 9:38 PM

All replies

  • User-760709272 posted

    Web services are platform agnostic, they only return text data and it is up to the client code to interpret that data.  You should ask on a PHP forum how you can consume a webservice, there is no such thing as a "c#" webservice or a ".net web service", it is an internet standard regarding communication.

    Friday, February 28, 2014 4:44 AM
  • User-999062906 posted

    I'm just trying to tell that my web service is written in c#.

    So here is what I found out:

    my web service return "List<Invoice>".

    the xml looks like this:

    <?xml version="1.0" encoding="utf-8"?>
    <ArrayOfInvoice xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
      <Invoice>
        <_strInvNo>INV001</_strInvNo>
        <_dtDate>2014-03-01T09:51:33.5864815+08:00</_dtDate>
        <_strVehicleNumber>ddd</_strVehicleNumber>
      </Invoice>
      <Invoice>
        <_strInvNo>INV002</_strInvNo>
        <_dtDate>2014-03-01T09:51:33.5864815+08:00</_dtDate>
        <_strVehicleNumber>ddd</_strVehicleNumber>
      </Invoice>
    </ArrayOfInvoice>

    So here is what I did in my php:

    $client = new SoapClient($WebServiceLocation);
    $params = array("strVehicleNumber" => $_SESSION['VehicleNumber']);
    $result = $client->GetOutstandingInvoices($params)->GetOutstandingInvoicesResult;
    //$xml = simplexml_load_string($result->GetOutstandingInvoicesResult);
    echo "<div>".var_dump($result)."</div><br>";
    function obj2array($obj) {
    	$out = array();
    	foreach ($obj as $key => $val) {
    		switch(true) {
    			case is_object($val):
    				$out[$key] = obj2array($val);
    				break;
    			case is_array($val):
    				$out[$key] = obj2array($val);
    				break;
    			default:
    				$out[$key] = $val;
    		}
    	}
    	return $out;
    }
    
    $result = obj2array($result);
    echo "<div>" . var_dump($result) . "</div>";
    echo "<div>" . $result["Invoice"][0]["_strInvNo"] . "</div>";

    Hope this help others.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 28, 2014 9:38 PM