如何将 PHP 对象编码为 nuSOAP 响应?

发布于 2024-09-17 12:28:50 字数 1325 浏览 1 评论 0原文

我正在构建一个简单的 WebService。我有一个 User PHP 类和一个 webmethod getUser 来检索用户的信息。

我已经声明了一个复杂类型,一切似乎都运行良好。

$server->wsdl->addComplexType('User',
    'complexType','struct', 'all', '',
    array(  'id' => array('name' => 'id', 'type' => 'xsd:int'),
            'username' => array('name' => 'username','type' => 'xsd:string'),
            'password' => array('name' => 'password','type' => 'xsd:string'),
            'email' => array('name' => 'email','type' => 'xsd:string'),
            'authority' => array('name' => 'authority','type' => 'xsd:int'),
            'isActive' => array('name' => 'isActive','type' => 'xsd:int'),
            'area' => array('name' => 'area','type' => 'xsd:string')
    )
);
$server->register('ws_getUser',
    array('user_id' => 'xsd:integer'),
    array('user' =>  'tns:User'),
    $namespace,
    "$namespace#ws_getUser",
    'rpc',
    'encoded',
    'Retorna un usuario'
);

function ws_getUser($user_id){
    return new soapval('return', 'tns:User', getUser($user_id));
}

但是,在 getUser 函数上,我将用户信息作为关联检索。数组,而不是用户对象本身。

我想在 getUser 上做的是返回一个 User 实例,并让 nuSOAP 为我序列化它。这可能吗?

编辑:我尝试返回一个新的 User() 出于测试目的,但响应是

<user xsi:type="tns:User"/>

I'm building a simple WebService. I have a User PHP class and a webmethod getUser to retrieve a user's information.

I've declared a complexType and everything seems to work perfectly.

$server->wsdl->addComplexType('User',
    'complexType','struct', 'all', '',
    array(  'id' => array('name' => 'id', 'type' => 'xsd:int'),
            'username' => array('name' => 'username','type' => 'xsd:string'),
            'password' => array('name' => 'password','type' => 'xsd:string'),
            'email' => array('name' => 'email','type' => 'xsd:string'),
            'authority' => array('name' => 'authority','type' => 'xsd:int'),
            'isActive' => array('name' => 'isActive','type' => 'xsd:int'),
            'area' => array('name' => 'area','type' => 'xsd:string')
    )
);
$server->register('ws_getUser',
    array('user_id' => 'xsd:integer'),
    array('user' =>  'tns:User'),
    $namespace,
    "$namespace#ws_getUser",
    'rpc',
    'encoded',
    'Retorna un usuario'
);

function ws_getUser($user_id){
    return new soapval('return', 'tns:User', getUser($user_id));
}

However, on the getUser function I'm retrieving the user info as an assoc. array, not the User Object itself.

What I would like to do on getUser is to return a User instance instead, and have nuSOAP serialize it for me. Is this possible?

Edit: I've tried returning a new User() for testing purposes, but the response is

<user xsi:type="tns:User"/>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薔薇婲 2024-09-24 12:28:50

我想我在这里找到了可能适用于这种情况的答案: http: //www.php.net/manual/en/class.soapserver.php

如果你想返回自定义对象
来自 nusoap web 服务的数组,您
必须将对象转换为数组
像这样:

<?php
$users = array();
while($res = $db_obj->fetch_row())
{
  $user = new user();
  $user->Id = $res['id'];
  $user->Username = $res['username'];
  $user->Email = $res['email'];

  $users[] = (array) $user;
}

return ($users);

Guess I've found out an answer that may apply to this case here: http://www.php.net/manual/en/class.soapserver.php

If you want to return a custom object
array from a nusoap webservice, you
have to cast the objects to arrays
like so:

<?php
$users = array();
while($res = $db_obj->fetch_row())
{
  $user = new user();
  $user->Id = $res['id'];
  $user->Username = $res['username'];
  $user->Email = $res['email'];

  $users[] = (array) $user;
}

return ($users);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文