如何使用返回.net中多维数组的php web服务

时间:2013-07-01 11:07:00

标签: c# php .net web-services nusoap

我在php中创建了一个Web服务,它在多维数组中返回数据,但问题是如何在.net中使用它。 我知道要使用一维数组返回数据但不知道多维数组的web服务,我已经包含下面的代码请帮帮我

服务器端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 个答案:

答案 0 :(得分:1)

朋友我得到了解决方案,现在工作正常。我只是在服务器端php文件中进行了更改

$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;

}

及其工作......:)