简体   繁体   English

PHP和Perl CGI脚本之间的通信

[英]Communication between PHP and Perl CGI scripts

I have two scripts that carry out different tasks on a server. 我有两个脚本可以在服务器上执行不同的任务。 One is written in Perl (.cgi) the other in PHP. 一种是用Perl(.cgi)编写的,另一种是用PHP编写的。

I am trying to send out a request from the perl CGI script by doing something like this: 我正在尝试通过以下操作从perl CGI脚本发出请求:

$ua = LWP::UserAgent->new;
$ua->agent("$0/0.1 " . $ua->agent);
$ua->timeout(30);

$queryStr = (xxxMaskedxxx);
$request = HTTP::Request->new('GET', $queryStr);
$response = $ua->request($request);
if ($response->is_success)
{
        $search = strpos($res->content, "not");
        if($search==true)
        { return -1; }
}

I tried two ways to send back the result from PHP: 我尝试了两种从PHP返回结果的方法:

This: 这个:

HttpResponse::setCache(true);
HttpResponse::setContentType('text/html');
if (!$result)
        HttpResponse::setData("<html>Message not delivered</html>");
else
        HttpResponse::setData("<html>Message successfully delivered</html>");
HttpResponse::send();

And this: 和这个:

echo "Content-type: text/html\n\n";
if (!$result)
      echo 'Message not delivered' . PHP_EOL;
else
      echo 'Message successfully delivered' . PHP_EOL;

But $response->is_success returns false for both case? 但是$response->is_success在两种情况下都返回false? When I try to print the response out, I am getting this: 当我尝试打印响应时,我得到了:

response is HTTP::Response=HASH(0x97a8b34)

What have I done wrong? 我做错了什么?

Also the two scripts are sitting side by side. 而且这两个脚本并排放置。 Are there any better ways to communicate between them? 他们之间有更好的沟通方式吗?

Perl calling cli.php script with command line arguments, Perl使用命令行参数调用cli.php脚本,

#!/usr/bin/perl

my $content = `/usr/bin/php cli.php xxxMaskedxxx`;

print $content;

cli.php echoing back received argument cli.php回显收到的参数

<?php

// output first argument from command line
print $argv[1];

But $response->is_success returns false for both case? 但是$ response-> is_success在两种情况下都返回false?

In both cases, you are outputting the default HTTP status , which is "200 OK". 在这两种情况下,您都将输出默认的HTTP状态 ,即“ 200 OK”。 You need to output a status code that indicates failure for is_success to fail. 您需要输出一个状态代码该状态代码指示is_success失败的失败。

When I try to print the response out, I am getting this 当我尝试打印响应时,我得到了这个

That's an HTTP::Response object . 那是一个HTTP::Response对象 You need to examine $response->decoded_content if you want to get the text out of it. 如果$response->decoded_content中获取文本,则需要检查$response->decoded_content

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM