通过循环的结果从PHP .NET Web服务调用 [英] Loop through result from a .NET web service call in php

查看:73
本文介绍了通过循环的结果从PHP .NET Web服务调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作得到结果形成一个返回泛型列表的方法的.NET Web服务。在PHP页面中使用的var_dump(这是调用使用WSDL的.NET方法),我能看到的是从.NET Web服务返回以下内容:

I'm working on getting results form a .net web service with a method that returns a generic list. Using var_dump in the php page (which is calling the .net method using WSDL), I was able to see the following is returned from the .net web service:

object(stdClass)#4 (1) { 
    ["testClass"]=> array(2) { 
        [0]=> object(stdClass)#5 (2) { 
            ["City"]=> string(7) "Hello_1" 
            ["State"]=> string(8) "World!_1" 
        } 
        [1]=> object(stdClass)#6 (2) { 
            ["City"]=> string(7) "Hello_2" 
            ["State"]=> string(8) "World!_2" 
        } 
    }
} 

这可能是一个愚蠢的问题,但我被困在处理。第(循环通)这个结果在PHP?我一定要建立在PHP类的TestClass也?

This might be a silly question but I'm stuck in procesing (looping through) this result in php? Do I have to create the class "testClass" in php also?

下面是.NET Web服务code

Here is the .net webservice code

public class testClass
    {
        public string City;
        public string State;
    }

[WebMethod]
public List<testClass> testAspMethod(string Param1, string Param2) 
{
    List<testClass> l = new List<testClass>();

    l.Add(new testClass { City = Param1 + "_1", State = Param2 + "_1" });
    l.Add(new testClass { City = Param1 + "_2", State = Param2 + "_2" });

    return l;
}

下面是PHP code调用该.NET Web服务

Here is the php code that calls this .net web service

$客户=新SoapClient的(HTTP://testURL/MyTestService.asmx WSDL);

$client = new SoapClient("http://testURL/MyTestService.asmx?WSDL");

$params->Param1 = 'Hello'; 
$params->Param2 = 'World!';

$result = $client->testAspMethod($params)->testAspMethodResult;

var_dump($result);

如何通过循环在PHP的结果?

How can I loop through the results in php?

推荐答案

这将工作无论项目数退还。如果你不这样做数组检查像我这里有,你的code会产生意想不到的效果。如果Web服务仅返回单个项目,也不会返回数组。相反,它实际上会直接把对象 $ result-&GT;的TestClass ,例如 $ result-&GT; testClass-&GT;城市不是 $ result-&GT;的TestClass [0] - &GT;城市如您所料。

This will work regardless of the number of items returned. If you don't do the array check like I have here, your code will produce unexpected results. If the webservice returns only a single item, it won't return an array. Instead it will actually put the object directly in $result->testClass, e.g. $result->testClass->City not $result->testClass[0]->City as you might expect.

// here we make sure we have an array, even if there's just one item in it
if(is_array($result->testClass))
{
    $result = $result->testClass;
}
else
{
    $result = array($result->testClass);
}

foreach($result as $item)
{
    echo 'City: ' . $item->City . '<br />';
    echo 'State: ' . $item->State . '<br />';
}

这篇关于通过循环的结果从PHP .NET Web服务调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆