Skip to content Skip to sidebar Skip to footer

Calling Python From Within Php Using Shell_exec

My default Web-Application is based on PHP. However, for easiness, I have built a python script that does some analysis. Now I need the php to call the python code and retrieve the

Solution 1:

Did you try running /usr/bin/python /var/www/include/sCrape.py -u '$my_url' in a shell? The mistake is probably there.

Try:

$cmd = "/usr/bin/python /var/www/include/sCrape.py -u '$my_url' 2>&1";
$response = shell_exec($cmd);
echo$response;

This should output an error message.

Why?

shell_exec only returns output from the standard output (stdout), if an error occurs it's written "to the" standard error (stderr). 2>&1 redirects stderr to stdout. See In the shell, what does “ 2>&1 ” mean?.

Run python code

You may want to add #!/usr/bin/env python on the first line of your python script and make it executable chmod +x /var/www/include/sCrape.py. Afterwards you should be able to run your script without explicitly calling python. /var/www/include/sCrape.py -u '$my_url'

Post a Comment for "Calling Python From Within Php Using Shell_exec"