PHP连接WCF服务解析返回数据
我在解析 WCF Web 服务返回的数据时遇到问题。
Web 服务正在传回一个字符串数组,该数据被放入 StdClass 对象中,我遇到的问题是数据的变化取决于是否有 1 个或多个对象。
从未处理过 stdclass 对象,我不太确定该怎么做。
以下是我当前使用的代码,$containers 是 Web 服务调用的返回值。
<ul>
<?php var_dump($containers)?>
<?php foreach($containers as $item):?>
<li>
<?php
echo $item->string;
?>
</li>
<?php endforeach;?>
</ul>
如果仅返回 1 个值,则以下代码可以正常工作并显示返回的容器名称。 如果返回的值超过 1 个,则 $item->string 变为数组。有没有办法确定 stdclass 包含哪些值?
var_dump 仅包含 1 个容器
object(stdClass)[13]
public 'GetContainersResult' =>
object(stdClass)[14]
public 'string' => string 'container1' (length=10)
var_dump 包含超过 1 个容器
object(stdClass)[13]
public 'GetContainersResult' =>
object(stdClass)[14]
public 'string' =>
array
0 => string 'container1' (length=10)
1 => string 'container2' (length=10)
提前致谢,
Matt
I'm having a problem parsing the data being returned by my WCF web service.
The web service is passing back an array of strings, this data is put into a StdClass object, the problem i'm encountering is that the data changes depending on whether there is 1 or more objects.
Having never dealt with stdclass objects I'm not really sure what to do.
The following is the code i'm currently using, $containers is the return value from the web service call.
<ul>
<?php var_dump($containers)?>
<?php foreach($containers as $item):?>
<li>
<?php
echo $item->string;
?>
</li>
<?php endforeach;?>
</ul>
If there is just 1 value being returned then the following code works fine and displays the returned container name.
If there is more than 1 value being returned $item->string becomes Array. is there anyway to determine what values stdclass contains?
var_dump with just 1 container
object(stdClass)[13]
public 'GetContainersResult' =>
object(stdClass)[14]
public 'string' => string 'container1' (length=10)
var_dump with more than 1 container
object(stdClass)[13]
public 'GetContainersResult' =>
object(stdClass)[14]
public 'string' =>
array
0 => string 'container1' (length=10)
1 => string 'container2' (length=10)
Thanks in advance,
Matt
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 is_array($item->string) 来检查是否有数组,然后对其进行适当的处理。根据您的代码,我认为这样的东西可能适合您。
you can use
is_array($item->string)
to check if you have an array and then process it appropriately. Based on your code, I think something like this might work for you.