问题描述
我有一个 WCF 操作 GetColors,它返回一个颜色列表作为 GetColorsResult.我得到的结果很好,但是如何在 php 中循环遍历 GetColorsResult 并回显每个元素?
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?
我在做:
<?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.
?>
有没有办法控制结果的名称,例如我没有指定WCF返回GetColorsResult,它在我的方法调用中附加了Result.同样,它将响应附加到 GetColors 以获取响应 (GetColorsResponse)
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)
执行print_r($retval)时的输出:
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
)
)
)
)
推荐答案
关于你的 print_r 这应该给你所有的价值:
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 />";
}
?>
这是你需要的吗?
BR,
TJ
如果您只是出于调试目的需要它,则应该使用 print_r.看看这里:print_r PHP 文档
If you just need it for debugging purposes, you should use print_r.Have a look here: print_r PHP Documentation
这篇关于从 php 中的 wcf 服务回显 xml 结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!