7

How can I call a PHP script from a Perl script and get its output as a variable?

3 Answers 3

5

Using the backtick operator:

my $phpOutput = `/usr/bin/php-cli your-script.php`;

Note that you may have to edit the path to point to your php executable.

If you want to have the output as a stream you can also open with a pipe (Perl <3):

open PHPOUT, "/usr/bin/php-cli your-script.php|";
while (<PHPOUT>) {
  # do something with the current line $_
}

See perldoc -f open.

Sign up to request clarification or add additional context in comments.

1 Comment

i did and nothing came up. it is too simple for google, i guess. I am a php guy, as you might have guessed and perl is like a totally different universe for me. thanks.
1

The reverse of this question, but the same answer.

Use backticks or the qx operator:

$output = `/path/to/php my_script.php`;

1 Comment

I prefer using readpipe to backticks, I think it makes the intent clearer.
1

It may be easier to distill this into its core problem, how to invoke another program from perl, which is answered in the perlop manpage's info about qx (or look up the perl qx command through some other means). That informs you how to run an external program and get the output, assuming that your PHP script is actually functional when called through the command line (can you run it via php your-php-script.php ?).

If your script is only functional through an HTTP request, then you need to use something like the LWP::UserAgent or WWW::Mechanize to get the content through its URL, similarly to how you would need to use HTTP_Request.php in PHP.

1 Comment

LOL, thanks for the community update almost 8 years after the fact. I'm much better at distinguishing it's/its now, and it was painful to see what it looked like in its prior state in the comparison page. I assume it really set off someone to get this treatment this much later on a small question, so sorry for that, but I'm not going to complain if someone makes me look better retroactively. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.