Is it possible to run a PHP script using python?
2 Answers
You can look into the subprocess class, more specifically, subprocess.call()
subprocess.call(*popenargs, **kwargs)
subprocess.call(["php", "path/to/script.php"]);
3 Comments
habnabit
TypeError; subprocess.call takes a list of strings as its first argument.allanwright
@habnabit Note the square brackets. We're making a tuple with the command followed by the arguments. For example:
cmd="php sandwiches.php --param yum"; subprocess.call(cmd.explode());habnabit
@AWrightIV see stackoverflow.com/posts/3784156/revisions (also, strings don't have an
explode method and square brackets make lists, not tuples)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.
3 Comments
habnabit
os.system should never be used; the subprocess module replaces it.sheki
I did not know that? Why is that ?
gahooa
subprocess implements many more features and safety checks. os.system works fine if you have a totally static (hardcoded) command, but if you have args, etc... it is a huge security hazard.