在Perl中发出HTTP GET请求的最简单方法是什么? [英] What's the simplest way to make a HTTP GET request in Perl?

查看:192
本文介绍了在Perl中发出HTTP GET请求的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些用PHP编写的代码,用于使用我们简单的Web服务,我也想在Perl中为可能更喜欢该语言的用户提供这些代码。制作HTTP请求的最简单方法是什么?在PHP中我可以用 file_get_contents()在一行中完成。

I have some code I've written in PHP for consuming our simple webservice, which I'd also like to provide in Perl for users who may prefer that language. What's the simplest method of making a HTTP request to do that? In PHP I can do it in one line with file_get_contents().

这是我要移植的整个代码to Perl:

Here's the entire code I want to port to 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;
}


推荐答案

LWP ::简单:

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

这篇关于在Perl中发出HTTP GET请求的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆