I’m working on getting results form a .net web service with a method that returns a generic list. Using var_dump in the php page (which is calling the .net method using WSDL), I was able to see the following is returned from the .net web service:
object(stdClass)#4 (1) {
["testClass"]=> array(2) {
[0]=> object(stdClass)#5 (2) {
["City"]=> string(7) "Hello_1"
["State"]=> string(8) "World!_1"
}
[1]=> object(stdClass)#6 (2) {
["City"]=> string(7) "Hello_2"
["State"]=> string(8) "World!_2"
}
}
}
This might be a silly question but I’m stuck in procesing (looping through) this result in php? Do I have to create the class “testClass” in php also?
Here is the .net webservice code
public class testClass
{
public string City;
public string State;
}
[WebMethod]
public List<testClass> testAspMethod(string Param1, string Param2)
{
List<testClass> l = new List<testClass>();
l.Add(new testClass { City = Param1 + "_1", State = Param2 + "_1" });
l.Add(new testClass { City = Param1 + "_2", State = Param2 + "_2" });
return l;
}
Here is the php code that calls this .net web service
$client = new SoapClient(“http://testURL/MyTestService.asmx?WSDL”);
$params->Param1 = 'Hello';
$params->Param2 = 'World!';
$result = $client->testAspMethod($params)->testAspMethodResult;
var_dump($result);
How can I loop through the results in php?
This will work regardless of the number of items returned. If you don’t do the array check like I have here, your code will produce unexpected results. If the webservice returns only a single item, it won’t return an array. Instead it will actually put the object directly in
$result->testClass, e.g.$result->testClass->Citynot$result->testClass[0]->Cityas you might expect.