Is it possible to run a PHP script using python?
Brian Webster
31k51 gold badges158 silver badges227 bronze badges
asked Sep 24, 2010 at 4:08
usertest
27.7k30 gold badges75 silver badges95 bronze badges
2 Answers 2
You can look into the subprocess class, more specifically, subprocess.call()
subprocess.call(*popenargs, **kwargs)
subprocess.call(["php", "path/to/script.php"]);
answered Sep 24, 2010 at 4:13
NullUserException
85.8k31 gold badges212 silver badges239 bronze badges
Sign up to request clarification or add additional context in comments.
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.
answered Sep 24, 2010 at 4:29
sheki
9,47814 gold badges56 silver badges70 bronze badges
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.default