I have some code written in PHP, but I have also developed a script written in Python. Is it possible to call this Python script from the PHP code?
If yes, how can I pass parameters to the Python script from the PHP?
I have tried to find an answer without any success.
Can someone give me a clue?
To call a Python file from within a PHP file, you need to call it using the shell_exec function.
You can run a python script via php, and outputs on browser. Basically you have to call the python script this way: $command = "python /path/to/python_script.py 2>&1"; $pid = popen( $command,"r"); while( ! feof( $pid ) ) { echo fread($pid, 256); flush(); ob_flush(); usleep(100000); } pclose($pid);
The new PyScript project lets you embed Python programs directly in HTML pages and execute them within the browser without any server-based requirements.
You use the system function: http://php.net/manual/en/function.system.php
Something like this:
$mystring = system('python myscript.py myargs', $retval);
You can try this:
python scripts:
test.py: print "hello"
then php Scripts
index.php: $i =`python test.py`; echo $i;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With