How to run python script in php

One way to run python script in PHP.

1.
The team will help us with this. shell_exec

ABSPATH Is a constant in WordPress… If the engine is different use $ _SERVER with ‘DOCUMENT_ROOT’ or similar.

$ python = ABSPATH ‘wp-content / python / venv / bin / python3’;
$ file = ABSPATH ‘wp-content / python / script.py’;

$ command = escapeshellcmd($ python $ file);
$ output = shell_exec($ command);
echo $ output;

$ python Is the path to the interpreter. Here where you install it, usually it is installed on the server in ! / usr / bin / env, but you can.

$ file – path to the script file in python

2.
Passing arguments

$ python = ABSPATH ‘wp-content / python / venv / bin / python3’;
$ file = ABSPATH ‘wp-content / python / script.py’;

ob_start(); passthru($ python $ file ‘argument’); $ output = ob_get_clean();
echo $ output;

On the side python getting arguments:

import sys
print(sysargv)

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *