6

Is it possible to run a PHP script using python?

2 Answers 2

14

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

subprocess.call(*popenargs, **kwargs)

subprocess.call(["php", "path/to/script.php"]);
Sign up to request clarification or add additional context in comments.

3 Comments

TypeError; subprocess.call takes a list of strings as its first argument.
@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());
@AWrightIV see stackoverflow.com/posts/3784156/revisions (also, strings don't have an explode method and square brackets make lists, not tuples)
4

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

os.system should never be used; the subprocess module replaces it.
I did not know that? Why is that ?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.