如何格式化要由Perl读取的web服务响应


How to format web service response to be read by Perl?

我正在使用Perl从我构建的web服务中请求一些信息(纯文本)。使用浏览器访问时,信息会完美显示。不过,我是使用Perl处理响应的新手。我成功地完成了一些数据库操作,我的问题是用Perl而不是PHP处理响应。

我必须如何编码或格式化响应才能成功处理它并用Perl输出相同的纯文本?

编辑:

需要考虑的要点

  • 我使用的是CakePHP2.x(稳定版)
  • $response->content 带来了整个HTML文件(包括JavaScript和整个包)。我只想在Perl脚本中显示纯文本
  • Perl脚本在检查*$response->is_success*时失败,并直接转到打印"出现问题"

我的php脚本运行良好

PHP代码:

<?php
$this->layout = 'ajax';
$this->autoRender = false;
// Some database handling here with no problem...
echo "Plain text with info from database";
?>

Perl代码:

#!/usr/local/bin/perl
require LWP::UserAgent;
require HTTP::Request;
my $request = HTTP::Request->new(GET => $url);
my $userAgent = LWP::UserAgent->new;
$userAgent->timeout(3);
$userAgent->env_proxy;
my $response = $userAgent->request($request);
if ($response->is_success) {
    print "Success!'n";
    #should print plain text AS IS
}
else {
    print "something went wrong...'n";
    die $response->status_line;
}

检查$response->is_success后,您应该能够只获取的返回值

$response->decoded_content

并按照您认为合适的方式进行处理。