简体   繁体   English

如何将URL或变量从Perl传递给PHP?

[英]How can I pass a url or variable from Perl to PHP?

I have two existing scripts that work fine as individuals. 我有两个现有的脚本,可以很好地作为个人使用。

The main script is Perl. 主要脚本是Perl。 I wish to execute the PHP script from a sub in the Perl script. 我希望从Perl脚本的一个子目录执行PHP脚本。

Usually, the PHP script is just run via direct url eg http://me.com/phpscript.php?foo=bar 通常,PHP脚本仅通过直接URL运行,例如http://me.com/phpscript.php?foo=bar

I would like to just call the PHP script from the Perl and pass the foo var so, I don't need to hit the PHP script with my browser to process the data. 我只想从Perl调用PHP脚本并传递foo var,所以,我不需要用浏览器来访问PHP脚本来处理数据。

I am not talented enough to rewrite the PHP script to Perl. 我没有足够的才能将PHP脚本重写为Perl。

I tried exec("http://me.com/phpscript.php?foo=bar"); 我尝试了exec(“ http://me.com/phpscript.php?foo=bar”); and include and system to no avail. 并且包含和系统均无济于事。

I have read and searched but, found only solutions to call Perl from PHP. 我已经阅读并搜索了,但是,仅找到从PHP调用Perl的解决方案。

I really appreciate the great guidance I always fine here. 我真的很感谢我一直在这里提供的出色指导。

Seems like LWP::UserAgent should work for this scenario. 似乎LWP :: UserAgent应该适用于这种情况。

require LWP::UserAgent;

 my $ua = LWP::UserAgent->new;
 $ua->timeout(10);
 $ua->env_proxy;

 my $response = $ua->get('http://me.com/phpscript.php?foo=bar');

 if ($response->is_success) {
     print $response->decoded_content;  # or whatever
 }
 else {
     die $response->status_line;
 }

If the script is located on the local filesystem, you should be able to exec it directly using the php interpreter and the path to the file. 如果脚本位于本地文件系统上,则应该能够使用php解释器和文件路径直接执行该脚本。 To access it via the web, use the LWP package. 要通过网络访问它,请使用LWP软件包。

For example: 例如:

exec('/usr/bin/php', 'myscript.php', @arguments);

Note that command-line arguments are handled differently than URL arguments; 请注意,命令行参数的处理方式与URL参数的处理方式不同; your PHP script will probably need to be modified to use them correctly. 您的PHP脚本可能需要修改才能正确使用它们。

You can directly run a php file if you add #!PathToPhp 如果添加#!PathToPhp,则可以直接运行php文件

  ./myscript.php

in that file using argc or argv or args you can get arguments passed to this file most basic is args 在使用argc或argv或args的文件中,您可以获取传递给该文件的参数,最基本的是args

  #!/bin/php
  <?php
  foreach($args as $key => $value){
   echo "\n".$key.":".$value;

There is a CPAN package that aims to provide a bridge between PHP and Perl: 有一个CPAN软件包,旨在在PHP和Perl之间建立桥梁:

This class encapsulates an embedded PHP5 intepreter. 此类封装了嵌入式PHP5解释器。 It provides proxy methods (via AUTOLOAD) to all the functions declared in the PHP interpreter, transparent conversion of Perl datatypes to PHP (and vice-versa), and the ability for PHP to similarly call Perl subroutines and access the Perl symbol table. 它为PHP解释器中声明的所有功能提供代理方法(通过AUTOLOAD),将Perl数据类型透明转换为PHP(反之亦然),以及PHP能够类似地调用Perl子例程并访问Perl符号表的功能。

The goal of this package is to construct a transaparent bridge for running PHP code and Perl code side-by-side. 该软件包的目标是构建一个透明桥梁,用于并行运行PHP代码和Perl代码。

Not sure how stable this is though. 不知道这有多稳定。 See 看到

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

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