如何将 PHP 对象编码为 nuSOAP 响应?
我正在构建一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想我在这里找到了可能适用于这种情况的答案: http: //www.php.net/manual/en/class.soapserver.php
Guess I've found out an answer that may apply to this case here: http://www.php.net/manual/en/class.soapserver.php