[英]Unable To Show Data in PHP Using ASP.NET Web Service
我已經在ASP.NET
創建了一個簡單的Web服務,並希望該服務在PHP
應用程序中使用。 Web服務如下:
[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以檢索客戶詳細信息。 因此,要在PHP
使用此服務,我嘗試使用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
編程,並試圖找出搜索Google的問題。 我什至嘗試直接使用如下所示的Web服務屬性訪問數組索引,但似乎缺少某些內容或可能不是正確的方法:任何想法都將不勝感激
echo $item[1]->CustName;
echo $item[1]; //Even this
現在,我正在使用Soap Web服務獲取Xml
數據,如下所示:
<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>
更新1 :使用了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
,再次出現此錯誤- 嘗試獲取non-object的屬性 。
更新2 :再次使用var_dump($item)
, PHP
編程的結果如下:
<?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)
同樣,嘗試使用兩個循環來獲取如下值,但是它僅返回前兩個結果,並且數據庫中總共有10條記錄:
<?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
只需做一個var_dump($item);
在php中查看它們的數組結構如何。.目前我不知道響應對象是什么,但是例如,您可以這樣訪問鍵: echo $item->Customer->CustName;
如果它是數組響應,而不是對象,則: $item['Customer']['CustName'];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.