wsdl返回一组复杂类型

时间:2010-01-08 12:12:44

标签: wsdl xsd

我已经定义了一个Web服务,它将从我的mysql数据库中返回数据。

我已经在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);

1 个答案:

答案 0 :(得分:10)

我在搜索互联网后自己找到答案。

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

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

接下来,我们必须创建另一个新的数据类型,它是我们创建的数据类型的数组。我将其称为person数组,其代码如下:

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

并将函数编程为返回一些虚拟数据:

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

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

    return $peopleArray;
}

顺便说一句,对不起,我没有提到,但所有这些代码都是用PHP编写的。

希望这有助于某人。

相关问题