代码之家  ›  专栏  ›  技术社区  ›  davr

用Perl发出HTTP GET请求最简单的方法是什么?

  •  32
  • davr  · 技术社区  · 16 年前

    我有一些用PHP编写的代码来使用我们的简单WebService,我还想用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;
    }
    
    8 回复  |  直到 9 年前
        1
  •  61
  •   ddimitrov    16 年前

    LWP::简单:

    use LWP::Simple;
    $contents = get("http://YOUR_URL_HERE");
    
        2
  •  15
  •   Barry Brown    16 年前

    simple具有您要查找的功能。

    use LWP::Simple;
    $content = get($url);
    die "Can't GET $url" if (! defined $content);
    
        3
  •  6
  •   bmdhacks    16 年前

    看一看 LWP::Simple . 对于更复杂的查询,甚至 a book about it .

        4
  •  4
  •   szabgab Brandon Fosdick    11 年前

    我会用 LWP::Simple 模块。

        5
  •  2
  •   Dave Horner    11 年前

    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::简单地在我的机器上工作时,我就使用了这个。

        6
  •  1
  •   Dave Horner    11 年前

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

        7
  •  0
  •   Srihari Karanth    10 年前

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

    my $content = `GET "http://trackMyPhones.com/"`;
    
        8
  •  0
  •   tboz203    9 年前

    我想斯里哈里可能提到的是 Wget ,但是我实际推荐的(同样,在没有lsp::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保持沉默。否则,每次在stderr上都会得到curl的进度条输出。

    推荐文章