在 PHP Web 服务中返回对象数组
我想使用 nuSOAP v 1.114 在 PHP Web 服务中返回文章对象数组。这就是我设置 WSDL 的方式:
$server->wsdl->addComplexType(
'ArticleType',
'complexType',
'struct',
'all',
'',
array('articleId' => array('name'=>'articleId', 'type'=>'xsd:int'),
'heading' => array('name'=>'heading', 'type'=>'xsd:string'),
'text' => array('name'=>'text', 'type'=>'xsd:string')
)
);
$server->wsdl->addComplexType(
'ArrayOfArticleType',
'complexType',
'array',
'',
'SOAP-ENC:Array',
array(),
array(
array('ref' => 'SOAP-ENC:arrayType',
'wsdl:arrayType' => 'tns:ArticleType[]' // ArticleType[]
)
),
'tns:ArticleType'
);
我的 PHP Article 类非常简单:
class Article {
public $articleId;
public $heading;
public $text;
public function __construct($articleId, $heading, $text=NULL) {
$this->articleId = $articleId;
$this->heading = $heading;
$this->text = $text;
}
}
如果我只返回一个新的 Article 对象,如下所示:
function TestArrayReturn() {
$arr = new Article(12345, "Test heading", "Test text.");
//$arr2 = array($arr);
return $arr;
}
函数,注册为:
$server->register("TestArrayReturn", array(), array('return'=>'tns:ArticleType'), $namespace, $namespace."#TestArrayReturn", 'rpc', 'encoded', 'Test function');
工作正常,并返回文章,就好像它是一个数组一样。但是,如果我尝试返回 ArrayOfArticleType ( TestArrayReturn() 中的注释行),并将该函数注册为返回类型 tns:ArrayOfArticleType ,则它会失败并显示 Error: HTTP Error: HTTP headers 之后没有数据。
但是,如果我手动创建数组的数组,如下所示:
$arr = array("articleId"=>12345, "heading"=>"Test heading", "text"=>"Test text");
$arr2 = array("articleId"=>12345, "heading"=>"Test heading", "text"=>"Test text");
return array($arr, $arr2);
它有效!?我的 ArrayOfArticleType WSDL 描述有什么问题,不允许它正确序列化 ArticleType 对象,但正确序列化具有相同属性(“articleId”、“heading”、“text”)的关联数组?
对所有代码感到抱歉,但我觉得有必要查明我忽略的错误。感谢您的帮助,几天来我一直在努力为该服务设置 WSDL。
I want to return an array of article objects in a PHP web service, using nuSOAP v 1.114. This is how I set up the WSDL:
$server->wsdl->addComplexType(
'ArticleType',
'complexType',
'struct',
'all',
'',
array('articleId' => array('name'=>'articleId', 'type'=>'xsd:int'),
'heading' => array('name'=>'heading', 'type'=>'xsd:string'),
'text' => array('name'=>'text', 'type'=>'xsd:string')
)
);
$server->wsdl->addComplexType(
'ArrayOfArticleType',
'complexType',
'array',
'',
'SOAP-ENC:Array',
array(),
array(
array('ref' => 'SOAP-ENC:arrayType',
'wsdl:arrayType' => 'tns:ArticleType[]' // ArticleType[]
)
),
'tns:ArticleType'
);
My PHP Article class is very simple:
class Article {
public $articleId;
public $heading;
public $text;
public function __construct($articleId, $heading, $text=NULL) {
$this->articleId = $articleId;
$this->heading = $heading;
$this->text = $text;
}
}
If I return just a new Article Object, like this:
function TestArrayReturn() {
$arr = new Article(12345, "Test heading", "Test text.");
//$arr2 = array($arr);
return $arr;
}
the function, registered as:
$server->register("TestArrayReturn", array(), array('return'=>'tns:ArticleType'), $namespace, $namespace."#TestArrayReturn", 'rpc', 'encoded', 'Test function');
works fine, and returns the article as if it's an Array. However, if I try and return an ArrayOfArticleType (the commented line in TestArrayReturn() ), and register the function as return type tns:ArrayOfArticleType, then it fails with Error: HTTP Error: no data present after HTTP headers.
If, however, I create an ARRAY of ARRAYS manually, like so:
$arr = array("articleId"=>12345, "heading"=>"Test heading", "text"=>"Test text");
$arr2 = array("articleId"=>12345, "heading"=>"Test heading", "text"=>"Test text");
return array($arr, $arr2);
it works!? What is wrong with my ArrayOfArticleType WSDL description that doesn't allow it to correctly serialize ArticleType objects, but correctly serialize an associative array with the same properties ("articleId", "heading", "text") ??
Sorry for all the code, but I feel it's necessary to pinpoint the error I'm overlooking. Any help appreciated, I've been struggling with setting up the WSDL for this service for days.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我修正了这个错误。对于任何感兴趣的人来说,这似乎是 NuSOAP 中的一个错误。您必须将函数的 returnType 注册为 xsd:Array,以便它正确返回数组,即使正确的 WSDL 会将其构成为“tns:ArrayOfArticleType”。我在一些 Drupal 源代码中发现了这一点:
希望这可以帮助其他遇到同样问题的人。
I fixed this error. For anyone interested, this seems to be a bug in NuSOAP. You MUST register your returnType for the function as xsd:Array for it to correctly return the array, even though correct WSDL would constitute it as 'tns:ArrayOfArticleType'. I found this in some Drupal source code:
Hope this helps someone else who struggled with the same problem.
这是客户端和服务器的示例,说明如何使用 php Web 服务返回数组。这是一个很容易理解的示例
http://my-source-codes .blogspot.com/2011/10/php-web-service-return-array.html
Here is example both client and server for how to return an array with php web service.It is easy example to understand
http://my-source-codes.blogspot.com/2011/10/php-web-service-return-array.html