簡體   English   中英

如何為Perl使用SOAP API?

[英]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:db2dbResponsereturn元素。

換句話說:這繞過SOAP :: Lite並使用LWP和帶有XML的簡單POST請求,解析XML響應。 您將失去自動提取功能,必須手動處理返回數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM