[英]How to use a SOAP API for Perl?
我正在尝试在Perl中改编以下PHP代码 。 这是一个bioDBnet SOAP API。 我尝试按照SOAP :: WSDL模块上的示例进行操作(在提要下),但未连接。 我收到一条错误消息:
尝试1
#!perl -w
use SOAP::WSDL;
my $client = SOAP::WSDL->new(
wsdl => "http://biodbnet.abcc.ncifcrf.gov/webServices/bioDBnet.wsdl",
);
错误消息: cannot import document for namespace >http://schemas.xmlsoap.org/soap/encoding/< without location at /Library/Perl/5.16/SOAP/WSDL/Expat/WSDLParser.pm line 90.
尝试2
接下来,我尝试使用SOAP :: LITE模块。 我遵循了这里的示例代码(在6.b.客户端下)。
#!perl -w
use SOAP::Lite;
my $client = SOAP::Lite ->
service('http://biodbnet.abcc.ncifcrf.gov/webServices/bioDBnet.wsdl');
my %db2dbParams;
$db2dbParams{'input'} = "Gene Symbol";
$db2dbParams{'outputs'} = "Gene ID, Ensembl Gene ID";
$db2dbParams{'inputValues'} = "MYC,A1BG";
$db2dbParams{'taxonId'} = "9606";
$db2dbRes = $client->db2db(%db2dbParams);
print $db2dbRes;
上面的代码不显示任何内容。 如何使bioDBnet SOAP API适用于Perl?
不要使用SOAP :: Lite服务方法,大多数情况下它是行不通的。 手动构建肥皂结构。 我尝试尽可能多地遵循php xml,尽管大多数前缀不是必需的。
use strict;
use warnings;
use Data::Dumper;
use SOAP::Lite;
#use SOAP::Lite +trace=>'all';
$uri = 'urn:bioDBnet';
$proxy = 'http://biodbnet.abcc.ncifcrf.gov/webServices/biodbnetSoapServer.php';
$tns = 'urn:bioDBnet';
$soap = SOAP::Lite->new(uri => $uri,proxy => $proxy);
$soap->envprefix('SOAP-ENV');
$soap->encprefix('SOAP-ENC');
$soap->ns($tns,'tns1');
$soap->on_action(sub{$tns.'#db2db'});
@request = (SOAP::Data->name(db2dbParams => \SOAP::Data->value(
SOAP::Data->name(input => 'Gene Symbol'),
SOAP::Data->name(outputs => 'Gene ID, Ensembl Gene ID'),
SOAP::Data->name(inputValues => 'MYC,A1BG'),
SOAP::Data->name(taxonId => 9606),
))->type('ns1:db2dbParams'),
);
$db2db = $soap->db2db(@request);
if ($match = $db2db->match('/Envelope/Body/db2dbResponse')) {
print "match ok: $match\n";
$result = $db2db->result;
print Dumper($result);
} else {
print "match nok: $match\n";
}
这会从服务器生成所需的输出。
您的第二个脚本有效,除了db2db部分。 我的猜测是,使用SOAP :: LITE发送时,发送到服务器的SOAP XML信封状态不佳。
我在第二行启用了调试:
#!/usr/bin/perl -w
use SOAP::Lite +trace =>'debug';
#use SOAP::Lite;
my $client = SOAP::Lite->service('http://biodbnet.abcc.ncifcrf.gov/webServices/bioDBnet.wsdl');
$inputs = $client->getInputs();
print $inputs;
$input = "Gene Symbol";
$outputs = $client->getOutputsForInput($input);
print $outputs;
$dirOutputs = $client->getDirectOutputsForInput($input);
print $dirOutputs;
..从您的尝试2:
# doesn't work, the created XML envelope is not in a good shape
my %db2dbParams;
$db2dbParams{'input'} = "Gene Symbol";
$db2dbParams{'outputs'} = "Gene ID, Ensembl Gene ID";
$db2dbParams{'inputValues'} = "MYC,A1BG";
$db2dbParams{'taxonId'} = "9606";
$db2dbRes = $client->db2db(%db2dbParams);
print $db2dbRes;
..我也试图使用SOAP:Data重写它,但是失败了。
# doesn't work, the created XML envelope is not in a good shape
$db2dbRes = $client->db2db(
SOAP::Data->name("db2dbParams")->type("ns1:db2dbParams")->prefix("ns1:db2db")->uri("urn:bioDBnet") =>
SOAP::Data->type("string")->name("input" => "Gene Symbol"),
SOAP::Data->type("string")->name("inputValues" => "MYC,MTOR"),
SOAP::Data->type("string")->name("outputs" => "Gene ID, Affy ID"),
SOAP::Data->type("string")->name("taxonId" => "9606")
);
print $db2dbRes;
切换到PHP以查看工作要求。 我已经在PHP脚本上启用了调试功能,以打印请求标头并从PHP提取工作的XML请求,并将其作为PERL中的POST内容重用。 基本上,通过在SoapClient上添加trace参数,然后转储最后一个reqeust标头。
<?php
$wsdl = "http://biodbnet.abcc.ncifcrf.gov/webServices/bioDBnet.wsdl";
$client = new SoapClient($wsdl, ['trace' => 1]);
$inputs = $client->getInputs();
print $inputs;
$input = "Gene Symbol";
$outputs = $client->getOutputsForInput($input);
print $outputs;
$dirOutputs = $client->getDirectOutputsForInput($input);
print $dirOutputs;
$db2dbParams['input'] = "Gene Symbol";
$db2dbParams['outputs'] = "Gene ID, Ensembl Gene ID";
$db2dbParams['inputValues'] = "MYC,A1BG";
$db2dbParams['taxonId'] = "9606";
$db2dbRes = $client->db2db($db2dbParams);
print $db2dbRes;
echo "====== REQUEST HEADERS =====" . PHP_EOL;
var_dump($client->__getLastRequestHeaders());
echo "========= REQUEST ==========" . PHP_EOL;
var_dump($client->__getLastRequest());
echo "========= RESPONSE =========" . PHP_EOL;
var_dump($db2dbRes);
这会与XML一起打印标头,预期输出为:
Gene Symbol Gene ID Ensembl Gene ID
MYC 4609 ENSG00000136997
A1BG 1 ENSG00000121410
我将“工作中”的XML请求数据包含到$ message中,并执行POST请求。
#!/usr/bin/perl -w
use strict;
use LWP::UserAgent;
use HTTP::Request;
my $message = '<?xml version="1.0" encoding="UTF-8" ?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="urn:bioDBnet" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:db2db>
<db2dbParams xsi:type="ns1:db2dbParams">
<input xsi:type="xsd:string">Gene Symbol</input>
<taxonId xsi:type="xsd:string">9606</taxonId>
<inputValues xsi:type="xsd:string">MYC,A1BG</inputValues>
<outputs xsi:type="xsd:string">Gene ID, Ensembl Gene ID</outputs>
</db2dbParams>
</ns1:db2db>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>';
my $userAgent = LWP::UserAgent->new();
my $request = HTTP::Request->new(POST => 'http://biodbnet.abcc.ncifcrf.gov/webServices/biodbnetSoapServer.php'); # ?debug=1
$request->content($message);
$request->content_type("text/xml; charset=utf-8");
my $response = $userAgent->request($request);
if($response->code == 200) {
print $response->as_string;
}
else {
print $response->error_as_HTML;
}
最后:在标题输出旁边,我们终于得到了一些数据:
<?xml version="1.0" encoding="UTF-8" ?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://biodbnet.abcc.ncifcrf.gov/webServices/bioDBnet"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:db2dbResponse>
<return xsi:type="xsd:string">Gene Symbol Gene ID Ensembl Gene ID
MYC 4609 ENSG00000136997 A1BG 1 ENSG00000121410
</return>
</ns1:db2dbResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
现在,您所需要做的就是从$response->as_string
(可能使用LibXML)中提取ns1:db2dbResponse
的return
元素。
换句话说:这绕过SOAP :: Lite并使用LWP和带有XML的简单POST请求,解析XML响应。 您将失去自动提取功能,必须手动处理返回数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.