问题
I have a WCF Operation GetColors which returns a list of colors as GetColorsResult. I am getting the result fine, but how do I loop through GetColorsResult in php and echo each element?
I am doing:
<?php
header('Content-Type: text/plain');
echo "WCF Test\r\n\r\n";
// Create a new soap client based on the service's metadata (WSDL)
$client = new SoapClient('http://localhost:8181/Colors.svc?wsdl',array(
'login' => "test", 'password' => "test"));
$retval = $client->GetColors();
//Need to loop throuh $retval here
echo $retval->GetColorsResult; //This throws error.
?>
Is there way to control the name of the result, for example, I did not specify WCF to return GetColorsResult, it appended Result to my method call. Likewise, it appends Response to GetColors for the Response (GetColorsResponse)
Output when doing print_r($retval):
stdClass Object
(
[GetColorsResult] => stdClass Object
(
[Color] => Array
(
[0] => stdClass Object
(
[Code] => 1972
[Name] => RED
)
[1] => stdClass Object
(
[Code] => 2003
[Name] => BLUE
)
[2] => stdClass Object
(
[Code] => 2177
[Name] => GREEN
)
)
)
)
回答1:
regarding to your print_r this should give you all the values:
<?php
$colorResult = $retval->GetColorsResult;
foreach($colorResult->Color as $color){
echo $color->Code . " " . $color->Name . "<br />";
}
?>
is this what you needed?
BR,
TJ
EDIT: If you just need it for debugging purposes, you should use print_r. Have a look here: print_r PHP Documentation
来源:https://stackoverflow.com/questions/6130476/echo-xml-result-from-wcf-service-in-php