代码之家  ›  专栏  ›  技术社区  ›  user8512043

无法使用ASP在PHP中显示数据。NET Web服务

  •  1
  • user8512043  · 技术社区  · 8 年前

    我在中创建了一个简单的web服务 ASP.NET PHP

    [WebMethod]
    [ScriptMethod(UseHttpGet = true)]
    public List<Customer> GetCustomers(int id)
    {
       id = Convert.ToInt32(HttpContext.Current.Request.QueryString["id"]);
    
       List<Customer> lst = null;
       using (var context = new DemoEntities())
       {
          lst = (from c in context.Customer
                 where c.CustomerID == id
                 select c).ToList();
       }
    
       return lst;
    }
    

    在上述web服务中,传递客户id以检索客户详细信息。因此,要在 ,我尝试使用 nuSoap 该库如下所示,几乎可以正常工作:

    <?php
        require_once("lib/nusoap.php"); //Using the nuSoap library
    
        $client   = new nuSoap_Client('http://localhost:1284/MyCustomers.asmx?wsdl', TRUE); //Passed the ASP.NET web service and an object created
        $result = $client->call('GetCustomers', array('id' => 1)); //Called the GetCustomers method and passed a default parameter
    
        foreach($result as $item) //Tried to iterate in a foreach loop
        {
          echo $item; //Here is the issue - The output returns or returned only the name 'Array'  
        }
    ?>
    

    PHP 很早以前就开始编程,并试图在搜索谷歌时解决这个问题。我甚至试着像下面这样直接使用web服务属性访问数组索引,但似乎遗漏了一些东西,或者可能不是正确的方法:如果有任何想法,我们将不胜感激

    echo $item[1]->CustName;
    echo $item[1]; //Even this 
    

    现在,我得到了 Xml 使用以下数据 肥皂 web服务:

    <ArrayOfCustomer>
      <Customer>
        <CustomerID>2</CustomerID>
        <CustName>John</CustName>
        <CustAddress>On Earth</CustAddress>
        <CustLocation>On Earth</CustLocation>
        <CustSex>Male</CustSex>
        <CustType>3</CustType>
        <CustStatus>2</CustStatus>
        <CustDetails>Great guy - Always regular.</CustDetails>
      </Customer>
    </ArrayOfCustomer> 
    

    :已使用 var_dump($item) 目前得到的数组元素如下:

    array
      'Customer' => 
        array
          'CustomerID' => string '2' (length=1)
          'CustName' => string 'John' (length=7)
          'CustAddress' => string 'On Earth' (length=25)
          'CustLocation' => string 'On Earth' (length=10)
          'CustSex' => string 'Male' (length=4)
          'CustType' => string '3' (length=1)
          'CustStatus' => string '2' (length=1)
          'CustDetails' => string 'Great guy - Always regular.' (length=47)
    

    $item->Customer->CustName ,再次获取此错误- 尝试获取非对象的属性

    var_转储($项) 结果如下: 编程:

    <?php
        require_once("lib/nusoap.php"); 
    
        $client   = new nuSoap_Client('http://localhost:1284/MyCustomers.asmx?wsdl', TRUE);
        $result = $client->call('GetAllCustomers'); //Without any parameter
        foreach($result as $item)
        {
           echo var_dump($item);
        }
    ?>
    

    输出 :

    array
      'Customer' => 
        array
          0 => 
            array
              'CustomerID' => string '1' (length=1)
              'CustName' => string 'Jack' (length=7)
              'CustAddress' => string 'On Earth' (length=25)
              'CustLocation' => string 'On Earth' (length=10)
              'CustSex' => string 'Male' (length=4)
              'CustType' => string '3' (length=1)
              'CustStatus' => string '2' (length=1)
              'CustDetails' => string 'Regular Customer and always happy to cooperate.' (length=47)
          1 => 
            array
              'CustomerID' => string '2' (length=1)
              'CustName' => string 'John' (length=4)
              'CustAddress' => string 'On Earth' (length=7)
              'CustLocation' => string 'On Earth' (length=10)
              'CustSex' => string 'Male' (length=4)
              'CustType' => string '3' (length=1)
              'CustStatus' => string '2' (length=1)
              'CustDetails' => string 'Great guy - Always regular.' (length=25)
    

    <?php
        require_once("lib/nusoap.php"); 
    
        $client   = new nuSoap_Client('http://localhost:1284/MyCustomers.asmx?wsdl', TRUE);
        $result = $client->call('GetAllCustomers');
        $count = count($result);
    
        foreach($result as $item)
        {
          for($i = 0; $i <= $count; $i++)
          {
             echo 'Name: ' . $item['Customer'][$i]['CustName'].'<br/>';
          } 
        }
    ?>
    

    :

    Name: Jack //Returns only first two records though it should return all the records
    Name: John
    
    1 回复  |  直到 8 年前
        1
  •  2
  •   Amjo    8 年前

    简单地做一个 var_dump($item); echo $item->Customer->CustName;

    如果是数组响应,而不是对象,则: $item['Customer']['CustName'];