tags:

views:

87

answers:

3

Is it possible to run a PHP script using python?

Thanks in advance.

+1  A: 

You can look into the subprocess class, more specifically, subprocess.call()

subprocess.call(*popenargs, **kwargs)

subprocess.call(["php", "path/to/script.php"]);
NullUserException
`TypeError`; `subprocess.call` takes a list of strings as its first argument.
Aaron Gallagher
+4  A: 

You can use the urllib2 library

import urllib2

urllib2.urlopen("http://sitename.com/script.php")

That is assuming the script is on another server.

If its on the same then the following should do it:

os.spawnl(os.P_NOWAIT, "php yourscript.php")

Using the os module.

Alternatively, as NullUser mentioned, you can use a sub process call:

import subprocess

def php(script_path):
    p = subprocess.Popen(['php', script_path] )
CoffeeBug
If it's the 2nd scenario, you should use the `subprocess`, class - it's meant to replace `os` for this purpose.
NullUserException
A: 

You can use the Python OS module. You can run any script by calling

os.system('php -f file.php')

The issue would be getting return values from PHP to Python here.

sheki
`os.system` should never be used; the `subprocess` module replaces it.
Aaron Gallagher
I did not know that? Why is that ?
sheki