简体   繁体   English

如何从Perl调用PHP脚本?

[英]How can I invoke a PHP script from Perl?

如何从Perl脚本调用PHP脚本并将其输出作为变量?

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. 请注意,您可能必须编辑指向php可执行文件的路径。

If you want to have the output as a stream you can also open with a pipe (Perl <3): 如果要将输出作为流,也open使用管道open (Perl <3):

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

See perldoc -f open . perldoc -f open

The reverse of this question , but the same answer. 这个问题的反面,但答案相同。

Use backticks or the qx operator: 使用反引号qx运算符:

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

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). 可能更容易将其提炼为其核心问题, 如何从perl调用另一个程序 ,这在perlop联机帮助页的qx信息中得到了perlop (或通过其他方式查找perl qx命令)。 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 ?). 这告诉你如何运行外部程序并获得输出,假设你的PHP脚本在通过命令行调用时实际上是有效的(你可以通过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. 如果您的脚本仅通过HTTP请求起作用,那么您需要使用类似LWP :: UserAgent或WWW :: Mechanize的内容来通过其URL获取内容,类似于在PHP中使用HTTP_Request.php的方式。

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

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