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. I wish to execute the P开发者_StackOverflowHP script from a sub in the Perl script.
Usually, the PHP script is just run via direct url e.g. 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.
I am not talented enough to rewrite the PHP script to Perl.
I tried 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.
I really appreciate the great guidance I always fine here.
Seems like LWP::UserAgent should work for this scenario.
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;
}
You can directly run a php file if you add #!PathToPhp
./myscript.php
in that file using argc or argv or args you can get arguments passed to this file most basic is args
#!/bin/php
<?php
foreach($args as $key => $value){
echo "\n".$key.":".$value;
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. To access it via the web, use the LWP
package.
For example:
exec('/usr/bin/php', 'myscript.php', @arguments);
Note that command-line arguments are handled differently than URL arguments; your PHP script will probably need to be modified to use them correctly.
There is a CPAN package that aims to provide a bridge between PHP and Perl:
This class encapsulates an embedded PHP5 intepreter. 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.
The goal of this package is to construct a transaparent bridge for running PHP code and Perl code side-by-side.
Not sure how stable this is though. See
- PHP::Interpreter
- Integrating PHP and Perl
精彩评论