Skip to content Skip to sidebar Skip to footer

1:1 Call PHP From Python

We're using Splunk (A tool to analyse machine data like log files) and have an application in PHP. For some data we need to do a call to our application in php (CLI-based). Unfortu

Solution 1:

Using Popen from the subprocess module:

import sys
from subprocess import Popen
output = subprocess.Popen(['php', 'path/to/script.php'] + sys.argv[1:], stdout=subprocess.PIPE).communicate()[0]

sys.argv[1:] contains every command line argument except the name of python script itself.


Solution 2:

Using subprocess.call:

import sys, subprocess
sys.exit(subprocess.call(['php', sys.argv[0].replace('.py', '.php')] + sys.argv[1:]))

Edit: Made Python script return the value the PHP script returned.


Post a Comment for "1:1 Call PHP From Python"