TelKitty
TelKitty

Reputation: 3156

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.

I am trying to send out a request from the perl CGI script by doing something like this:

$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:

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? 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?

Upvotes: 1

Views: 592

Answers (2)

Quentin
Quentin

Reputation: 943510

But $response->is_success returns false for both case?

In both cases, you are outputting the default HTTP status, which is "200 OK". You need to output a status code that indicates failure for is_success to fail.

When I try to print the response out, I am getting this

That's an HTTP::Response object. You need to examine $response->decoded_content if you want to get the text out of it.

Upvotes: 1

mpapec
mpapec

Reputation: 50637

Perl calling cli.php script with command line arguments,

#!/usr/bin/perl

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

print $content;

cli.php echoing back received argument

<?php

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

Upvotes: 1

Related Questions