[ACCEPTED]-How do I include a PHP script in Python?-execution

Accepted answer
Score: 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

0

Score: 7

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

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

Score: 1

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

Score: 0

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

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

Score: 0

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

More Related questions