【问题标题】:Echo xml result from wcf service in php?php中wcf服务的回显xml结果?
【发布时间】:2011-05-25 20:28:07
【问题描述】:

我有一个 WCF 操作 GetColors,它返回一个颜色列表作为 GetColorsResult。我得到的结果很好,但是如何在 php 中循环 GetColorsResult 并回显每个元素?

我在做:

<?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)

执行 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
                    )
               )
          )
      )

【问题讨论】:

  • 你可能想要添加一些你已经拥有的代码......(顺便说一下没有投票)
  • 我很乐意添加代码。不知道为什么投反对票,我对php不熟悉。
  • 你可以试试 print_r($retval) 并发布输出吗?
  • @TerrenceJackson,我更新了我的帖子以显示 print_r($retval) 结果

标签: php wcf


【解决方案1】:

关于你的 print_r 这应该给你所有的价值:

<?php
$colorResult = $retval->GetColorsResult;
foreach($colorResult->Color as $color){
  echo $color->Code . " " . $color->Name . "<br />";
}
?>

这是你需要的吗?

BR,

TJ

编辑: 如果你只是需要它来进行调试,你应该使用 print_r。 看看这里:print_r PHP Documentation

【讨论】:

  • 我完全按照你说的做了,它没有抛出错误,但它只是打印出 11223330000,但我只是在做:echo $color->Name + "
    "
  • 我删除了
    并且它工作了,但现在一切都在一条线上。
  • 我必须做一个 .而不是 + 用于连接。谢谢。

  • 是html中的换行符。你到底想要什么?类似于 ;
  • 我修复了它,但我必须这样做。而不是 +
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多