If you want to call a python script from PHP the quick & dirty way, then this post is for you. We will not install any thing other than standard PHP & Python, no mod_python or mod_wsgi messed up. I’m using this method most of the time for small functionality that I don’t want to spend time on building up a complete new project.
There are many way to call a Python script from a PHP file, in this post I will show you using the backtick operator, other options like popen, system, exec, etc .. I will cover later on
So suppose you have Python interpreter is working & a script that you want to call from PHP, the python script can accept one or more arguments.
Here is what I often come up with
123456 $myArgs = $_POST["args"]; # receive from any where, here I got arguments from a form POST$myArgs = escapeshellcmd($myArgs); # for security, escape the string to avoid command injection$myCommand = "/path/to/python /path/to/python/script.py ".$myArgs;$result = `$myCommand`#Now you can do anything with the result here