我试图通过Perl使用SOAP访问Web服务,并且在调用需要参数的服务函数时遇到问题.决定SOAP调用的XSD说,
<xs:complexType name="getVersion"> <xs:sequence/> </xs:complexType> <xs:complexType name="getVersionResponse"> <xs:sequence> <xs:element minOccurs="0" name="return" type="xs:string"/> </xs:sequence> </xs:complexType> ... <xs:complexType name="enumerateEntities"> <xs:sequence> <xs:element name="entityId" type="xs:int"/> <xs:element minOccurs="0" name="entityType" type="tns:entityType"/> </xs:sequence> </xs:complexType> <xs:complexType name="enumerateEntitiesResponse"> <xs:sequence> <xs:element maxOccurs="unbounded" minOccurs="0" name="return" nillable="true" type="xs:int"/> </xs:sequence> </xs:complexType>
根据服务的文档,这两个函数的签名是:
String getVersion () int[] enumerateEntities (int entityId,EntityType entityType)
#!/usr/bin/perl use SOAP::Lite; my $uri = 'http://wsdl.mydomain.com/'; my $service = SOAP::Lite -> uri($uri) -> on_action(sub { sprintf '"Call by on_action: %s"',shift}) -> proxy('http://192.168.1.100:8688/MyService/services/MyService.MyServicePort/'); $method = SOAP::Data->name("MyService")->attr({xmlns => $uri}); $getVersion = SOAP::Data->name("getVersion")->attr({xmlns=>$uri});#line 11 my $theResult = $service->getVersion; unless ($theResult->fault){ print "Version: "; print $theResult->result;} else {print $theResult->faultstring;}
…但是我通过更改第11行来调用带参数的函数的尝试(下面)是徒劳的.
... @entityId = SOAP::Data->type('int')->name('entityId')->value(0); @entityType = SOAP::Data->type('EntityType')->name('entityType')->value(NODE); $enumerateEntities = SOAP::Data->name("enumerateEntities",@entityId,@entityType)->attr({xmlns=>$uri}); my $result2 = $service->enumerateEntities; print $result2->result;
编辑:
这是使用SOAP :: WSDL的更新示例代码
#!/usr/bin/perl use SOAP::WSDL; use Data::Dumper; my $service = SOAP::WSDL->new( wsdl => 'http://192.168.1.100:8688/MyService/services/MyService.MyServicePort?wsdl',outputhash => 1 );
解决方法
使用
SOAP::WSDL来使用服务wsdl,它还可以为您提供和(可选)为纯perl数据结构进行编组.强烈推荐的模块.
类似于以下内容:
use SOAP::WSDL; use Data::Dumper; my $soap = SOAP::WSDL->new( wsdl => 'http://server/pathtoservice?WSDL',outputhash => 1 ); my $res = $soap->call('method',{ foo => 1,bar =>2 }); die $res->faultstring if $res->fault; print Dumper($res->result);