将perl代码转换为php? (2)

时间:2012-02-20 17:25:25

标签: php perl

这是perl代码...

use strict;
use warnings;
use LWP::UserAgent;
use HTTP::Date;

my $taxon = $ARGV[0]; # Taxonomy identifier of organism.

my $query = "http://www.uniprot.org/uniprot/?query=organism:$taxon&format=fasta&include=yes";
my $file = $taxon . '.fasta';

my $agent = LWP::UserAgent->new;
my $response = $agent->mirror($query, $file);

if ($response->is_success) {
    my $results = $response->header('X-Total-Results');
    my $release = $response->header('X-UniProt-Release');
    my $date = sprintf("%4d-%02d-%02d", HTTP::Date::parse_date($response->header('Last-Modified')));
    print "Downloaded $results entries of UniProt release $release ($date) to file $file\n";
}
elsif ($response->code == HTTP::Status::RC_NOT_MODIFIED) {
    print "Data for taxon $taxon is up-to-date.\n";
}
else {
    die 'Failed, got ' . $response->status_line .
        ' for ' . $response->request->uri . "\n";
}

我试过把它转换为php ... 仍然存在错误的代码行:在else if语句中 如果您发现其他错误,请在此处发表评论......

这是我到目前为止所尝试的内容

$taxon = $ARGV[0]; # Taxonomy identifier of organism.

$query = "http://www.uniprot.org/uniprot/?query=organism:$taxon&format=fasta&include=yes";
$file = $taxon . '.fasta';

$response = $agent->mirror($query, $file);

if ($response->is_success) {
  $results = $response->header('X-Total-Results');
  $release = $response->header('X-UniProt-Release');
  $date = sprintf("%4d-%02d-%02d", date_parse($response->header('Last-Modified')));
  print "Downloaded $results entries of UniProt release $release ($date) to file $file\n";
}
elsif ($response->code == HTTP::Status::RC_NOT_MODIFIED) {
  print "Data for taxon $taxon is up-to-date.\n";
}
else {
  die ('Failed, got ' . $response->status_line .
    ' for ' . $response->request->uri . "\n");
}

5 个答案:

答案 0 :(得分:3)

我不相信你可以在php,托盘elsif中使用else if

答案 1 :(得分:1)

elsif失败的原因是因为它需要是elseif。

答案 2 :(得分:1)

你在哪里运行这个脚本? $ARGV是perl在命令行中使用的。如果您从表单中获取信息,请使用发布或获取方法(使用$_POST$_GET访问)。

elsif替换为elseif

您可以调用$agent->mirror之类的内容,但这里没有定义它们。

正如quickshiftin指出的那样,你不能引用Perl库 - 你必须找到PHP等价物。

答案 3 :(得分:1)

以下教程可能有用: http://www.cs.wcupa.edu/~rkline/perl2php/

答案 4 :(得分:1)

使用此WebGet课程。

require("WebGet.php");
$taxon = $argv[0];
$agent = new WebGet();
$agent->useCache = true;
$agent->cacheLocation = '/tmp';
touch('cookie.txt');
$agent->cookieFile = 'cookie.txt';


$query = "http://www.uniprot.org/uniprot/?query=organism:$taxon&format=fasta&include=yes";
$file = $taxon . '.fasta';

$agent->requestContent($query);

if($agent->responseStatusCode==200){
    $results = $agent->responseHeaders[strtoupper('X-Total-Results')];
    $release = $agent->responseHeaders[strtoupper('X-UniProt-Release')];
    $date = date("Y-m-d", strtotime($agent->responseHeaders[strtoupper('Last-Modified')]));
    print "Downloaded $results entries of UniProt release $release ($date) to file $file\n";
} // 304 Not Modified
elseif($agent->responseStatusCode==304){
    print "Data for taxon $taxon is up-to-date.\n";
}else{
    die ('Failed, got ' . $agent->responseStatusLine .
    " for uniprot/?query=organism:$taxon&format=fasta&include=yes\n");
}