2010-01-08 145 views
0

我已经定义了一个Web服务,从我的MySQL数据库返回数据的数组。WSDL返回复杂类型

我已经用php编写了web服务。

现在我已经定义的复杂类型,如下所示:

$server->wsdl->addComplexType(
'Category', 
'complexType', 
'struct', 
'all', 
'', 
array(
    'category_parent_id' => array('name' => 'category_parent_id', 'type' => 'xsd:int'), 
    'category_child_id' => array('name' => 'category_child_id', 'type' => 'xsd:int'), 
    'category_list' => array('name' => 'category_list', 'type' => 'xsd:int') 
) 

);

上述复杂类型是我数据库中表中的一行。现在

我的功能必须把这些行的一组让我怎么实现同样

我的代码如下:

require_once('./nusoap/nusoap.php'); 
$server = new soap_server; 

$server->configureWSDL('productwsdl', 'urn:productwsdl'); 

// Register the data structures used by the service 
$server->wsdl->addComplexType(
    'Category', 
    'complexType', 
    'struct', 
    'all', 
    '', 
    array(
     'category_parent_id' => array('name' => 'category_parent_id', 'type' => 'xsd:int'), 
     'category_child_id' => array('name' => 'category_child_id', 'type' => 'xsd:int'), 
     'category_list' => array('name' => 'category_list', 'type' => 'xsd:int') 
    ) 
); 
$server->register('getaproduct',     // method name 
    array(),   // input parameters 
    //array('return' => array('result' => 'tns:Category')), // output parameters 
    array('return' => 'tns:Category'), // output parameters 
    'urn:productwsdl',       // namespace 
    'urn:productwsdl#getaproduct',     // soapaction 
    'rpc',         // style 
    'encoded',        // use 
    'Get the product categories'  // documentation 
); 

function getaproduct() 
{ 
    $conn = mysql_connect('localhost','root',''); 
    mysql_select_db('sssl', $conn); 
    $sql = "SELECT * FROM jos_vm_category_xref"; 
    $q = mysql_query($sql); 
    while($r = mysql_fetch_array($q)) 
    { 
     $items[] = array('category_parent_id'=>$r['category_parent_id'], 
           'category_child_id'=>$r['category_child_id'], 
           'category_list'=>$r['category_list']); 
    } 
     return $items; 
} 


    // Use the request to (try to) invoke the service 
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ''; 
$server->service($HTTP_RAW_POST_DATA); 
+0

是的,我知道这个格式是不正确的,但我的家伙在粘贴由编辑器提供的代码块中的代码,但它似乎并没有工作。 如果有些能告诉我为什么还是有人可以对其进行编辑对我来说这将是巨大的 – 2010-01-12 03:36:47

回答

10

我想通自己的答案搜索互联网之后。

以下是创建复杂数据类型的代码。在这里我创建一个数据类型的人有界河名字,年龄和性别作为其数据成员。

$server->wsdl->addComplexType(
    'Person', 
    'complexType', 
    'struct', 
    'all', 
    '', 
    array(
    'firstname' => array('name' => 'firstname', 'type' => 'xsd:string'), 
    'age'  => array('name' => 'age', 'type' => 'xsd:int'), 
    'gender' => array('name' => 'gender', 'type' => 'xsd:string') 
) 
); 

接下来,我们必须创建另一个新的数据类型,这是我们所创建的数据类型的数组。我把它叫做人阵和它的代码是如下:

$server->wsdl->addComplexType(
    'PersonArray', // Name 
    'complexType', // Type Class 
    'array',   // PHP Type 
    '',    // Compositor 
    'SOAP-ENC:Array', // Restricted Base 
    array(), 
    array(
     array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:Person[]') 
    ), 
    'tns:Person' 
); 

现在我注册了一个名为getPeople函数,它接受任何输入参数,但返回的人数组作为:

$server->register(
    'getPeople',       // method name 
    array(),        // input parameters 
    array('return' => 'tns:PersonArray'), // output parameters 
    'urn:hellowsdl2',      // namespace 
    'urn:hellowsdl2#getPeople',   // soapaction 
    'rpc',        // style 
    'encoded',       // use 
    'Return an array of people'   // documentation 
); 

和编程通过我很抱歉,我没有提及,但所有这些代码是PHP的方式

function getPeople() 
{ 
    $peopleArray = array(); 
    $peopleArray[] = array(
     'firstname' => "Anand", 
     'age'  => 25, 
     'gender' => "Male" 
    ); 

    $peopleArray[] = array(
     'firstname' => "Sandhya", 
     'age'  => 21, 
     'gender' => "Female" 
    ); 

    return $peopleArray; 
} 

:函数返回一些虚拟的数据。

希望这可以帮助别人。

+0

大,我的朋友,你的榜样帮助我的很多解决我的问题在Web服务中创建:-)的NuSOAP非常感谢您! – Cris 2010-07-21 13:05:33

+0

感谢您的回答。它帮助我解决问题。 – 2017-06-13 15:45:43