8

I have a PHP script (news-generator.php) which, when I include it, grabs a bunch of news items and prints them. Right now, I'm using Python for my website (CGI). When I was using PHP, I used something like this on the "News" page:

<?php
print("<h1>News and Updates</h1>");
include("news-generator.php");
print("</body>");
?>

(I cut down the example for simplicity.)

Is there a way I could make Python execute the script (news-generator.php) and return the output which would work cross-platform? That way, I could do this:

page_html = "<h1>News and Updates</h1>"
news_script_output = php("news-generator.php") //should return a string
print page_html + news_script_output
3
  • 1
    "I simplified the example for simplicity." -- What else would you simplify it for? ;) Commented Jun 29, 2009 at 20:46
  • @musicfreak: giggles? efficiency? beauty? Commented Jun 29, 2009 at 20:59
  • 2
    I've worked with people who "simplified the example" to obscure their incompetence. I hope that's not the case here. Commented Jun 29, 2009 at 23:19

5 Answers 5

11
import subprocess

def php(script_path):
    p = subprocess.Popen(['php', script_path], stdout=subprocess.PIPE)
    result = p.communicate()[0]
    return result

# YOUR CODE BELOW:
page_html = "<h1>News and Updates</h1>"
news_script_output = php("news-generator.php") 
print page_html + news_script_output
Sign up to request clarification or add additional context in comments.

1 Comment

The only issue is, as S.Lott mentioned, simulating the CGI environment that PHP wants.
7

PHP is a program. You can run any program with subprocess.

The hard part is simulating the whole CGI environment that PHP expects.

3 Comments

Good point. Depends what kind of PHP you're running, I suppose.
Does PHP-CLI expect this type of environment?
PHP CGI expects a lot of environment. The CLI appears to require much less environmental setup. Here's the definitive doc. php.net/manual/en/features.commandline.php.
1

maybe off topic, but if you want to do this in a way where you can access the vars and such created by the php script (eg. array of news items), your best best will be to do the exec of the php script, but return a json encoded array of items from php as a string, then json decode them on the python side, and do your html generation and iteration there.

Comments

0

I think the best answer would be to have apache render both pages separately and then use javascript to load that page into a div. You have the slight slowdown of the ajax load but then you dont have to worry about it.

There is an open-source widget thing that will run multiple languages in 1 page but I cant remember what its called.

Comments

0

You could use urllib to get the page from the server (localhost) and execute it in the right environment for php. Not pretty, but it'll work. It may cause performance problems if you do it a lot.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.