?
Solved

how to run a php script from perl

Posted on 2008-11-18
21
Medium Priority
?
841 Views
Last Modified: 2012-05-05
I have a perl script that is already set up and running properly through a cron job.  I want to add one task to it, and that is to run a php file.  This php file does exactly what I want it to if run through a browser.  But instead of manually running it, I want it to just execute in line with the perl tasks.

I've added the following first line to the php file:
#!/usr/local/bin/php

I've added the following line to the perl script (which is run by cron)
system('/home/my_user_directory/cron/autoresponder.php');

In the above scenario, the perl script runs but does not do the php script.  I've also tried the following, but I got a "could not open input file" even though I set the CHMOD to 777:
system('/home/my_user_directory/public_html/autoresponder.php');

What do I need to do differently to get that php script to run?  (also, please don't suggest to rewrite the script in perl ... way out of my league, and also I don't want to set up a separate cron for this script b/c it needs to stay within the other perl tasks). Thanks!
0
Comment
Question by:JoeFletcher
  • 11
  • 6
  • 2
  • +1
21 Comments
 
LVL 29

Expert Comment

by:Michael Worsham
ID: 22988772
0
 
LVL 39

Expert Comment

by:Adam314
ID: 22988784
If the php script uses any information that the webserver normally provides, you'll have to provide this - such as the QUERY_STRING, any other environment variables, or any data on STDIN.  What does the php script do?
0
 
LVL 1

Author Comment

by:JoeFletcher
ID: 22989074
The php script includes sql and connections to the database, etc. And it runs in a browser.  Does that mean if I move the location of the php file to public_html that it'll be able to get all those variables through its normal code within the php?  And if that's the case, could I simply use some of the coding suggested by mwecomputers to call the script?

The php script is pretty complex, so I wouldn't want to have to "pull stuff out" of there and put it into perl.  I just want to call the script from perl.  since it runs fine in a browser, I'm hoping this is possible.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:JoeFletcher
ID: 22989168
Looking through some of those suggested examples, could this work if I simply add this code to my perl script?
  use PHP::Interpreter;
 
  my $p = PHP::Interpreter->new();
  $p->include("/home/my_user_directory/public_html/autoresponder.php");

Open in new window

0
 
LVL 39

Expert Comment

by:Adam314
ID: 22989215
The code from mwecomputers will not solve any problems caused by the php script expecting a particular web server environment, if any.  

When you run the code through the webserver, the webserver will create some environment variables.  Also, if the php page is POSTed to (such as from a form on another page), the webserver will provide that POST info to the script through STDIN (standard input).

If the script expects any of this, then when you run the script from perl, you will have to provide it.

Also, if the php script uses any files on the system, which ever user runs it through the cron will need access to those files.


What are the exact errors you get?

Can you post the php script?
0
 
LVL 1

Author Comment

by:JoeFletcher
ID: 22989433
I'm glad you suggested posting the script b/c I don't really know how to answer your questions. The php file is not doing any kind autoresponder.php&id=99 or any jazz like that. Simply put, when I load www.myurl.com/autoresponder.php it runs the script behind the scenes, and the only output is a simple echo 'success' type message.

It does reference other files on the system. I'm not sure how I would give permission per se, but I've never run into a "you don't have permission" situation before.

I don't get any errors.  simply put, my perl script runs as normal, but doesn't seem to excecute the php file.




0
 
LVL 39

Expert Comment

by:Adam314
ID: 22989465
Using PHP::Interpreter, you would probably want to use eval, not include.  And if the php script expects any of the webserver environment, you will have to pass that in the call to new - see the section in the new function about GET, POST, COOKIE, SERVER, and ENV.
0
 
LVL 1

Author Comment

by:JoeFletcher
ID: 22989489
Here's the file, renamed .txt so I can upload it.

autoresponder.php.txt
0
 
LVL 1

Author Comment

by:JoeFletcher
ID: 22989520
sorry... that's a bit over my head... I'm not sure what you are suggesting... I'm really lost with the webserver enviroment GET eval etc etc stuff.  Could you give me an example of what my perl code might look like to get this to work?

I should mention, I'm not really a programmer... the best I can do (so far) is to tweak settings and fake my way through.  :(
0
 
LVL 39

Expert Comment

by:Adam314
ID: 22989532
Are you sure the script can find 'includes/application_top.php'?  I'm not to familiar with php, so I'm not sure where php looks for this.  This could be the problem, as when the cron runs the script, the current directory is likely different than when the webserver runs the script.

If you use PHP::Interpreter, you can specify the include path with the INCLUDE_PATH option to the new method.  If you start it with system, use a chdir so the file can be found before calling system.
0
 
LVL 1

Author Comment

by:JoeFletcher
ID: 22989651
The php file finds the application_top file with no problem when it is run from the browser. when I run it from perl, should I edit the php file to use all absolute paths, e.g.,

/home/my_user_directory/public_html/includes/application_top.php

Also, can you help me with what I currently have as my perl code and point out what to do differently:



#!/usr/bin/perl 
 
use strict;
use Net::FTP;
use DBI;
use PHP::Interpreter;
 
# Tons of code here yada yada
 
# code to run php file
my $p = PHP::Interpreter->new();
$p->include("/home/my_user_directory/public_html/autoresponder.php");

Open in new window

0
 
LVL 1

Author Comment

by:JoeFletcher
ID: 22990341
ok, when I ran this with PHP::Interpreter, my telnet said it couldn't find PHP::interpreter. So any way, trying a new route.  When I ran this code, I received no errors, but the php file still did not run.  I'm pretty sure my perl coding is incomplete. Can anyone help me complete this so that the php file runs?

Note: I don't need to send the php file any parameters, and I don't need any results back for the rest of my perl code.  I just need this one php file to execute - all by itself it will do what I need (it's code to send out emails).

#!/usr/bin/perl 
 
use strict;
use Net::FTP;
use DBI;
use LWP::UserAgent;
 
my $ua = LWP::UserAgent->new;
$ua->agent("PerlApplication/0.1");
 
HTTP::Request->new(GET => 'http://www.mysite.com/autoresponder.php');

Open in new window

0
 
LVL 1

Author Comment

by:JoeFletcher
ID: 22991844
Ok - let's try to start from scratch. Going back to my original post, I discovered why I was getting "could not open input file" message. In putty, I changed directories to my public_html diectory and then ran the php script, and it ran fine.  

But, I still can't get it to run in my perl script. I use the following code to switch to public_html, run the php file, and go back to my cron directory. but it doesn't work.  Any thoughts? (btw, I did print "We are in " . cwd . "\n"; to make sure I was changing directories)



#!/usr/bin/perl 
#!/usr/local/bin/php
 
use strict;
use Net::FTP;
use DBI;
use Cwd;
 
# Tons of perl code here that run fine yada yada
 
# Attempt to get into the public_html folder and run the php script
chdir("..");
chdir("public_html");
system("autoresponder.php");
chdir("..");
chdir("mycronfolder");
 
# Tons of perl code here that run fine yada yada

Open in new window

0
 
LVL 3

Expert Comment

by:saurabhpendharker
ID: 22991927
Try the following in perl script:
system("cd <TO public_html>; ./<php file>;cd <path to cron folder>");
0
 
LVL 39

Expert Comment

by:Adam314
ID: 22991965
>>couldn't find PHP::Interpreter
You need to install the module.  Use this as root:
    perl -MCPAN -e 'install PHP::Interpreter'

Using all absolute paths will be better.  I'm not familiar with php, so I can't help much there, and that is where I think the problem is.  Using either PHP::Interpreter or system should work.  Try running a very simple php file, and see if that works.  Something like:
    Start of php
    <% phpinfo() %>
    End of php

If that works, then you know the problem is somewhere within your php code.  If not, the problem is with perl starting php.

If the problem is in your php code, to help debug it, you could have your php code create a log file, and print to it every so often (every few lines).  Then after it runs, look at the log to see how much was executed.
0
 
LVL 1

Author Comment

by:JoeFletcher
ID: 22992366
@saurabhpendharker could you give me the exact language for that code? I'm confused by what's example vs actual.  Is it something like this:

  system("cd ./public_html; autoresponder.php; cd ./cronfolder;");    OR
  system("cd home/myuser/public_html; autoresponder.php; cd home/myuser/cronfolder;");

@Adam314 I got this error when trying to install PHP::Interpreter:
 CPAN: File::HomeDir loaded ok (v0.80)
 CPAN: Storable loaded ok (v2.18)
 mkdir /home/.cpan: Permission denied at /usr/lib/perl5/5.8.8/CPAN.pm line 4117

I'm on shared hosting... I'm probably not allowed to install something like that?
0
 
LVL 3

Accepted Solution

by:
saurabhpendharker earned 668 total points
ID: 22992553
You may try this:
system("cd /home/myuser/public_html; ./autoresponder.php; cd /home/myuser/cronfolder;");
0
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 668 total points
ID: 22994944
If you are using system, there is no need to cd after running autoresponder.php, as the entire command is done in a sub-shell, which has no effect on the parent
    system("cd /home/myuser/public_html; ./autoresponder.php");

As to installing the module, it appears that your CPAN is not setup correctly.  You will need to talk to your host about this.

0
 
LVL 29

Assisted Solution

by:Michael Worsham
Michael Worsham earned 664 total points
ID: 22998470
Since you are on shared hosting, about the only thing you can do is request they install the PHP::Interpreter CPAN module.

As for the call to the PHP file, do you need it to return a value (i.e. succeed or fail) or just execute?

exec("/usr/bin/php /path/to/autoresponder.php");

$result = system("/usr/bin/php /path/to/autoresponder.php");


Reference:
http://perl.about.com/od/programmingperl/qt/perlexecsystem.htm
http://www.geom.uiuc.edu/~scheftic/PMC/UseScripts/Perl/system.html
0
 
LVL 1

Author Comment

by:JoeFletcher
ID: 23000482
Ok I finally figured it out after days of trial and error and your help.  The main issue, I think, was differences in relative paths and fixed paths depending on whether I was doing it through cron, putty, browser.  The final solution that did the trick....

system("cd public_html; php autoresponder.php");

Thank you, everyone, for your help. I didn't think I was going to be able to get this one done, and I couldn't have done it without you.
0
 
LVL 1

Author Closing Comment

by:JoeFletcher
ID: 31518017
I was really impressed with how everyone stuck with this and troubleshooted hard to the end.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question