44

我有一些我用 PHP 编写的代码来使用我们的简单 web 服务,我还想在 Perl 中为可能更喜欢该语言的用户提供这些代码。发出 HTTP 请求的最简单方法是什么?在 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;
}
4

8 回答 8

77

LWP::简单:

use LWP::Simple;
$contents = get("http://YOUR_URL_HERE");
于 2008-09-25T17:59:43.163 回答
19

LWP::Simple 具有您正在寻找的功能。

use LWP::Simple;
$content = get($url);
die "Can't GET $url" if (! defined $content);
于 2008-09-25T18:02:39.677 回答
6

看看LWP::Simple。对于更多涉及的查询,甚至还有一本关于它的书

于 2008-09-25T17:59:29.530 回答
6

我会使用LWP::Simple模块。

于 2008-09-25T17:59:52.260 回答
3

试试HTTP::Request模块。此类的实例通常传递给 LWP::UserAgent 对象的 request() 方法。

于 2008-09-25T18:00:55.470 回答
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 在我的机器上工作时,我使用了它。

于 2013-09-20T04:23:51.610 回答
1

如果它在 Unix 中并且没有安装 LWP::Simple,您可以尝试:

my $content = `GET "http://trackMyPhones.com/"`;
于 2015-03-20T09:23:49.850 回答
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>

旗帜-s告诉 curl 保持沉默。否则,您每次都会在标准错误上获得 curl 的进度条输出。

于 2016-01-29T20:32:01.730 回答