Search code examples
phpherokushell-exec

failed to call a Python Script from PHP on Heroku platform(hosting)


i successfuly deployed my php app in Heroku platform. in my php code, i call Python file by shell_exec() as follows:

$result = shell_exec('python test.py ' . escapeshellarg($content));

my php app worked on localhost and there is no problem, the other functions of my php app worked on Heroku. But when i request the file which has this statement:

$result = shell_exec('python test.py ' . escapeshellarg($content));

the out put of python script is empty i.e echo $result; is empty. to be more clear, if i run testpython.php on my localhost the output in browser after run $result = shell_exec('python test.py ' . escapeshellarg($content)); will be : results : "the output here from python script". but when i run testpython.php from Heroku hosting the result is empty i checked if hosting supports shell_exec function by :

is_callable('shell_exec') && false === stripos(ini_get('disable_functions'), 
'shell_exec');

and the result was true. testpython.php is only run the python script on the shell and show the result in the browser.

If anyone may know the reason or have any comment/solution about that, it will be great to hear from him/her.

hint: I think the problem is that I am using some library in python script which is not available on Heroku server. is there a way to install python library (example nltk) on heroku server?


Solution

  • as I guessed the problem was that I am using some library in python script which is not available on Heroku server. because my app is PHP so the default buildpacks is PHP. finally, i used Multiple Buildpacks(one for PHP and another for python) to solve all unavailable libraries on heroku server. and now my application perfectly work and php script call remote python script