views:

80

answers:

4

I have two existing scripts that work fine as individuals.

The main script is Perl. I wish to execute the PHP 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.

A: 

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.

Wooble
Yes, they are in the same directory even. Everything else you said was over my head. Can you provide a verbose example for me?
DulcimerDude
This looks promising. Currently the $_GET["foo"] is in the php script. That is the only var that needs passed. How would I mod the PHP script and your example to pass and get foo?
DulcimerDude
A: 

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;
nerkn
Thank you but, I tried everything on this page and the PHP script does not execute. I need a full example I guess.
DulcimerDude
@DulcimerDude: See: http://www.php.net/manual/en/features.commandline.usage.php for how to call PHP scripts from the command line. This way, you can just call these PHP scripts from any system call within your Perl scripts.
bob-the-destroyer
A: 

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

Gordon
I will try this. I have tried all above and nothing is working for me. All this just to pass one var. This is very difficult for me.
DulcimerDude
Nope. I am stumped.
DulcimerDude
@Dulcimer well, what exactly is not working? Will the script run but not receive input? If it runs, could it be you are trying to get `bar` with `$_GET['foo']` instead of [`$argv`](http://www.php.net/manual/en/reserved.variables.argv.php)?
Gordon
+1  A: 

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;
 }
bob.faist