Echo xml result from wcf service in php?

微笑、不失礼 提交于 2020-01-24 23:02:45

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!