2013-07-01 53 views
0

我在php中創建了一個web服務,它返回多維數組中的數據,但問題是如何在.net中使用它。 我知道使用Web服務在一個維數組返回數據,但不知道多維數組,我已經包含下面的代碼,請幫我如何使用php webservice返回多維數組in .net

服務器端的php文件

<?php 
require_once('lib/nusoap.php'); 

$server = new soap_server(); 
$server->configureWSDL('servCarRent', 'urn:servCarRent'); 

$server->wsdl->addComplexType('getAllKeyData','complexType','array','all','', 
array(
    'KeyId'=> array('name'=>'KeyId', 'type' =>'xsd:int'), 
    'KeyName'=> array('name'=>'KeyName', 'type' =>'xsd:string'), 
    'IsAvailable'=> array('name'=>'IsAvailable', 'type' =>'xsd:int'), 
    'NoOfKeys'=> array('name'=>'NoOfKeys', 'type' =>'xsd:int') 
    ) 
); 

$server->wsdl->addComplexType(
    'MySoapObjectArray', 
    'complexType', 
    'array', 
    '', 
    'SOAP-ENC:Array', 
    array(), 
    array(array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:getAllKeyData[]')), 
    'tns:getAllKeyData' 
); 

$server->register(
    'getAllKeys', 
    array(), 
    array('return'=>'tns:MySoapObjectArray'), 
    $ns, 
    false, 
    'rpc', 
    false, 
    'Processes an array of MySoapObjects and returns one of them'); 


function getAllKeys() 
{ 
    $con=mysql_connect('localhost','root','pass')or die("cannot connect"); 

    mysql_select_db('mydb')or die("cannot select db"); 

    $sql = 'Select * from KeyTable'; 
    $result=mysql_query($sql,$con); 
    $out=array(); 
    while($row = mysql_fetch_assoc($result)) 

    { 
     $out[]=$row; 
    } 

    return $out; 


} 


$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ''; 
$server->service($HTTP_RAW_POST_DATA); 


exit(); 

?> 

客戶端PHP文件

<?php 
require_once('lib/nusoap.php'); 

$client = new nusoap_client('http://www.mydomainname.com/new/servCarRent.php?wsdl'); 

$err = $client->getError(); 
if ($err) { 

    echo '<h2>Constructor error</h2><pre>' . $err . '</pre>'; 

} 
$response = $client->call('getAllKeys'); 

if($client->fault) 
{ 
    echo '<h2>Fault</h2><pre>'; 
    echo '</pre>'; 
} 
else 
{ 
    $err = $client->getError(); 
    if ($err) { 

     echo '<h2>Error</h2><pre>' . $err . '</pre>'; 
    } 
    else 
    { 

echo '<h2>Result</h2><pre>'; 
     print_r($response); 
    echo '</pre>'; 

} 
} 

?> 

如果用完客戶端文件然後我可以訪問數據,如下圖所示

Result 

Array 
(
    [0] => Array 
     (
      [0] => 1 
      [1] => 20577369Y 
      [2] => 1 
      [3] => 2 
     ) 

    [1] => Array 
     (
      [0] => 2 
      [1] => 63337921S 
      [2] => 1 
      [3] => 2 
     ) 



) 

,但我想訪問它在.NET中,我已經包含在我的.NET項目Web引用,並把它命名爲webSerCarRent,我已經試過類似如下,但給我的XML文檔

webSerCarRent.servCarRent scr = new webSerCarRent.servCarRent(); 
      webSerCarRent.getAllKeyData[] gakd = new webSerCarRent.getAllKeyData[10]; 

      gakd = scr.getAllKeys(); 
      for (int i = 0; i < gakd.Length; i++) 
      { 

       string kname = gakd[i].KeyName; 
      } 

錯誤,請幫我

回答

1

朋友我得到了解決方案,現在它的工作正常。我只是做了在服務器端的PHP文件的變化,其中i更換

$server->wsdl->addComplexType('getAllKeyData','complexType','array','all','', 
array(
    'KeyId'=> array('name'=>'KeyId', 'type' =>'xsd:int'), 
    'KeyName'=> array('name'=>'KeyName', 'type' =>'xsd:string'), 
    'IsAvailable'=> array('name'=>'IsAvailable', 'type' =>'xsd:int'), 
    'NoOfKeys'=> array('name'=>'NoOfKeys', 'type' =>'xsd:int') 
    ) 
); 

$server->wsdl->addComplexType('getAllKeyData','complexType','struct','all','', 
array(
    'KeyId'=> array('name'=>'KeyId', 'type' =>'xsd:int'), 
    'KeyName'=> array('name'=>'KeyName', 'type' =>'xsd:string'), 
    'IsAvailable'=> array('name'=>'IsAvailable', 'type' =>'xsd:int'), 
    'NoOfKeys'=> array('name'=>'NoOfKeys', 'type' =>'xsd:int') 
    ) 
); 

和而.net文件訪問它,出現了一個錯誤,在.net文件無差錯代碼如下

webSerCarRent.servCarRent scr = new webSerCarRent.servCarRent(); 
webSerCarRent.getAllKeyData[] gakd = new webSerCarRent.getAllKeyData[10]; 
for (int i = 0; i < gakd.Length; i++) 
{ 
    gakd[i] = scr.getAllKeys()[i]; 
    int id = gakd[i].KeyId; 
    string kname = gakd[i].KeyName; 

} 

及其工作... :)