【问题标题】:What's the simplest way to make a HTTP GET request in Perl?在 Perl 中发出 HTTP GET 请求的最简单方法是什么?
【发布时间】:2008-09-25 17:55:57
【问题描述】:

我有一些使用 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;
}

【问题讨论】:

    标签: perl web-services http


    【解决方案1】:

    LWP::简单:

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

    【讨论】:

    • 所以...我们有四个回复提到 LWP::Simple,我想这就是要使用的一个
    • 对于像 'URL' 这样的静态字符串,最好使用单引号来节省 perl 查找要插入的内容的工作。
    • 对于静态字符串,双引号没有真正的开销。使用单引号的原因是为了让下一个程序员清楚他们不需要在代码中寻找插值。
    【解决方案2】:

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

    use LWP::Simple;
    $content = get($url);
    die "Can't GET $url" if (! defined $content);
    

    【讨论】:

      【解决方案3】:

      看看LWP::Simple。 对于更多涉及的查询,甚至还有a book about it

      【讨论】:

        【解决方案4】:

        我会使用LWP::Simple 模块。

        【讨论】:

          【解决方案5】:

          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 在我的机器上工作时,我使用了它。

          【讨论】:

            【解决方案6】:

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

            【讨论】:

              【解决方案7】:

              如果是 Unix 并且没有安装 LWP::Simple,你可以试试:

              my $content = `GET "http://trackMyPhones.com/"`;
              

              【讨论】:

              • 什么是“GET”?它没有安装,也不能在我的 Ubuntu 14.04 Linux 上运行。
              • GET 是随LWP Perl 库一起安装的实用程序。
              【解决方案8】:

              我认为 what Srihari might be referencingWget,但我实际上建议(再次,在没有 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 的进度条输出。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2011-12-19
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2019-11-30
                • 1970-01-01
                相关资源
                最近更新 更多