在Perl中,最简单的发起HTTP GET请求的方法是什么?

48

我已经用PHP编写了一些代码来消费我们的简单Web服务,我还想将其提供给可能更喜欢使用Perl语言的用户。那么最简单的方法是什么?在PHP中,我可以使用file_get_contents()函数一行代码完成。

这里是我想要移植到Perl的整个代码:

/**
 * Makes a remote call to the our API, and returns the response
 * @param cmd {string} - command string ID
 * @param argsArray {array} - associative array of argument names and argument values
 * @return {array} - array of responses
 */
function callAPI( $cmd, $argsArray=array() )
{
   $apikey="MY_API_KEY";
   $secret="MY_SECRET";
   $apiurl="https://foobar.com/api";

   // timestamp this API was submitted (for security reasons)
   $epoch_time=time();

   //--- assemble argument array into string
   $query = "cmd=" .$cmd;
   foreach ($argsArray as $argName => $argValue) {
       $query .= "&" . $argName . "=" . urlencode($argValue);
   }
   $query .= "&key=". $apikey . "&time=" . $epoch_time;

   //--- make md5 hash of the query + secret string
   $md5 = md5($query . $secret);
   $url = $apiurl . "?" . $query . "&md5=" . $md5;

   //--- make simple HTTP GET request, put the server response into $response
   $response = file_get_contents($url);

   //--- convert "|" (pipe) delimited string to array
   $responseArray = explode("|", $response);
   return $responseArray;
}
8个回答

80

LWP::Simple:

use LWP::Simple;
$contents = get("http://YOUR_URL_HERE");

1
所以……我们得到了四个提到 LWP::Simple 的回应,我想那就是要使用的。 - davr
2
对于像 'http://URL' 这样的静态字符串,最好使用单引号以避免 Perl 查找要插入的内容。 - AndrewJFord
10
对于静态字符串而言,使用双引号并没有实际的开销。使用单引号的原因在于让下一个程序员清楚,他们不需要在代码中寻找插值。 - Dave Rolsky

20
LWP::Simple有你需要的功能。
use LWP::Simple;
$content = get($url);
die "Can't GET $url" if (! defined $content);

6

6

3

尝试使用HTTP::Request模块。通常,该类的实例会传递给LWP::UserAgent对象的request()方法。


3

Mojo::UserAgent 也是一个很好的选择!

  use Mojo::UserAgent;
  my $ua = Mojo::UserAgent->new;

  # Say hello to the Unicode snowman with "Do Not Track" header
  say $ua->get('www.☃.net?hello=there' => {DNT => 1})->res->body;

  # Form POST with exception handling
  my $tx = $ua->post('https://metacpan.org/search' => form => {q => 'mojo'});
  if (my $res = $tx->success) { say $res->body }
  else {
    my ($err, $code) = $tx->error;
    say $code ? "$code response: $err" : "Connection error: $err";
  }

  # Quick JSON API request with Basic authentication
  say $ua->get('https://sri:s3cret@example.com/search.json?q=perl')
    ->res->json('/results/0/title');

  # Extract data from HTML and XML resources
  say $ua->get('www.perl.org')->res->dom->html->head->title->text;`

从CPAN页面直接获取示例。当我无法在我的机器上使用LWP::Simple时,我使用了这个。


1
如果在Unix系统中,且未安装LWP::Simple模块,您可以尝试以下操作:
my $content = `GET "http://trackMyPhones.com/"`;

3
“GET”是什么?它没有安装在我的Ubuntu 14.04 Linux上,也无法工作。 - matt freake
2
GET 是安装在 LWP Perl 库中的实用工具。 - reinierpost

1

我认为Srihari可能在引用什么Wget, 但我实际上会推荐(在没有LWP::Simple的*nix上)使用cURL

$ my $content = `curl -s "http://google.com"`;
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>
< p > -s 标志告诉 curl 保持静默。否则,每次都会在标准错误输出 curl 的进度条。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接