Calling Python from within PHP using shell_exec

前端 未结 1 375
梦如初夏
梦如初夏 2021-01-15 03:02

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 retri

相关标签:
1条回答
  • 2021-01-15 03:28

    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'

    0 讨论(0)
提交回复
热议问题