PHP Embedded In Python

K

Keith

I am a python and php newbie. Newbie is even pushing it.

I need to take the contents of my_cgi.py and evaluate it with the
php_module. Is this possible. I think that pyPHP goes the other way.

The only reason I need this is because the header that all the html
pages use on our website uses a php include and I really don't want to
rewrite all the php in python.

Thanks,
Keith
 
J

jdonnell

I'm not sure exactly what your trying to do, but I use php and python
together a lot. I usually call the python script from php with the
passthru function like this:

<?php
$outputFromPy = passthru('/path/to/myScript.py');
?>
 
K

Keith

jdonnell said:
I'm not sure exactly what your trying to do, but I use php and python
together a lot. I usually call the python script from php with the
passthru function like this:

<?php
$outputFromPy = passthru('/path/to/myScript.py');
?>

I need to go the other direction. I need to call php from within
python. Thanks.
 
J

jdonnell

Well it depends on how php is installed. Is it a linux system? Do you
know how to run a php script from the command line?

Once you have the php script running from the command line then you
need to use the os module in python. There are a couple different ways
to do it. Read the following for more info.
http://docs.python.org/lib/os-process.html
http://docs.python.org/lib/os-newstreams.html#os-newstreams

simple example:

import os
handle = os.popen('path/to/yourScript.php', 'r')
print handle.read()
 
K

Keith

Well it depends on how php is installed. Is it a linux system? Do you
know how to run a php script from the command line?

I'm running on Fedora Core 3.

To run php command line I just ran:
% php <php.script>
It spit something back at me.

Maybe I should lay out what I am doing a little more.

I have a website.

At the top of each html page in the website it looks like:
------------------
<?php
include "header.html"
?>

..... HTML CODE ....

<?php
include "footer.html"
?>
-------------------



In "header.html" it looks like:
-------------------
<?php
include "access.php"; // A login cookie type thingy
?>
.... HTML CODE FOR LOGO, COMMON MENU ETC...
-------------------


As the story goes... I decided to put a Wiki on the website. I got
MoinMoin which is a Python based Wiki. It's really cool. MoinMoin
uses a python cgi to generate html pages. MoinMoin configuration
allows you to put an optional header and footer for each displayed
page. Straight html works fine; however, my "header.html" and
"footer.html" do not with the embedded php. The resulting python
generated pages have the literal string <?php include "access.php"; ?>.

Maybe the answer is still to use the os call.

Thanks for your help. My little brush with the Python community gives
me the feeling that you guys are friendly. It's nice.
 
J

jdonnell

Ok, I'm still a little confused. You mention header.html and
access.php.

For access.php use the os call. You'll probably want to use popen, and
you also need to change your php script slightly. In order to run php
scripts from the command line you have to put
#!/usr/bin/php
as the first line in the php script. You then need to make sure that
it's executable. Run this from the command line:
chmod 755 access.php
Now you can call it from python with os.popen('/path/to/access.php')
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,219
Messages
2,571,118
Members
47,731
Latest member
unutbu

Latest Threads

Top