我在ASP.NET
中創建了一個簡單的Web服務,並且希望在PHP
應用程序中使用該服務。該Web服務如下:無法使用ASP.NET Web服務在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以檢索客戶詳細信息。因此,在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
前編程很長的時間,並試圖找出問題的搜索谷歌。我甚至已經嘗試與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>
更新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
嘗試,再次收到此錯誤 - 試圖讓非對象的財產。
更新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
我能夠看到使用數組元素'變種_dump($ item)'但是當用這個'$ item-> Customer-> CustName'嘗試時,再次得到這個錯誤 - **嘗試獲取非對象**的屬性。感謝您的快速建議。看到我更新的帖子。 – user8512043
我更新了我的答案:) – Amjo
太棒了!非常感謝,並完美工作。學到了很多東西:) – user8512043