Skip to content Skip to sidebar Skip to footer

How Do I Include A Php Script In Python?

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 P

Solution 1:

import subprocess

defphp(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

Solution 2:

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

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

Solution 3:

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.

Solution 4:

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.

Post a Comment for "How Do I Include A Php Script In Python?"