relocation error on running python script from php - php

I'm trying to run my python script from php. I get the following error
python: relocation error: python: symbol SSL_load_error_strings, version OPENSSL_1.0.0 not defined in file libssl.so.1.0.0 with link time reference
I'm using import socket in python script which is causing the error.
Any help with would be much appreciated.
php_code -
shell_exec('python C:/xampp/htdocs/webInterface/php_ex/pyth_1.py aa 2>&1');

Xampp comes with it's own version of the common libraries.
When you run Python from PHP, the environment variables are set to find Xampp libs, and their versions might differ from what Python is expecting.
On unix, to see what is different run from PHP:
shell_exec('/usr/bin/env');
Then compare the output of the same command from a terminal window.
For me, unsetting the LD_LIBRARY_PATH was all I needed to get Python working:
shell_exec("LD_LIBRARY_PATH='' python myscript.py");

Related

exec('php path/to/script.php') 'php' is not recognized as an internal or external command

I have this code, as part of a php script:
exec("php runScript.php --task_id {$taskId} 2>&1 ", $scriptOutput);
Where scriptoutput returns: 'php' is not recognized as an internal or external command.
I have php in my systemvariables and when I run
php runScript.php --task_id 1
on the commandline it works without problems and i can use the php command for other things such as php composer.phar install.
But when my php script arrives at the rule with the exec code, it returns that php isnt recognized. The script works on Ubuntu, but not locally on my Windows environment. So perhaps it's a Windows issue.
I read a lot about environmentvariables but if that was the case i wasn't able to use php in the command line. Now it's only not working via exec.
I hope someone can help me with this.

'python' is not recognized as an internal or external command, operable program or batch file using PHP passthru()

I am trying to invoke my python script from php by using passthru() function. I have already done that successfuly and for development I used xampp , now at some moment I installed manually apache,php and other add-ons.
I also made changes to apache conf to make my python scripts work, some of them work when i invoke them directly via ajax,but scripts like this:
<?php
passthru("python C:/Apache24/htdocs/app1/pyscripts/export_zakup.py GARČIN
2>&1",$retval);
echo $retval;
?>
give me result like:
'python' is not recognized as an internal or external command, operable program or batch file. 1
Also when i copy python C:/Apache24/htdocs/app1/pyscripts/export_zakup.py GARČIN to my cmd it works properly.
I have already spent few hours trying to find out where the problem is but unsuccessfuly. Anyone knows where the problem is?
First, locate where the Python executable is by runniing which python from the command line:
$ which python
$ /usr/bin/python
Then use the full path to the executable in your PHP script:
passthru("/usr/bin/python C:/Apache24/htdocs/app1/pyscripts/export_zakup.py GARČIN
2>&1",$retval);
echo $retval;
Keep in mind that the path will be different on Windows vs. Linux based machines. For example the latest versions on Windows typically install the executable in C:\Python27\, so the full path would be C:\Python27\python

Running complicated ROS shell command through php

I'm trying to run a ROS shell program on the server through php on Ubuntu 14.04. I have tried using system, exec, shell_exec but nothing happens and I don't get any output. The system call is the following:
echo shell_exec("/opt/ros/indigo/bin/rosrun gazebo_ros spawn_model -database Part_A -gazebo -model Part_A");
What are the limitations of using system or exec to run any shell command through php on a server?
I don't care as much about the output of the command as for its execution. I think the problem has to do with the fact that PHP doesn't have any PATH like shell does so it can't find any applications without specifying the exact location. How can I make PHP se the same PATH shell does?
The problem was that the apache user and the environment in which the bash commands are running are not set up correctly. I followed the instructions from this answer but instead of using "source" I used "." and instead of using the source.bash file I used the source.sh file. I also set all the environment variables that had to do with ros or gazebo using the putenv() function.

python script executed from php gives error

On my debian i run xampp. I want to execute a python script using php shell_exec.
This is my php code:
shell_exec("/opt/lampp/htdocs/news/hello.py 2>1 &");
When i run it from the browser i get this error:
/usr/bin/python: /opt/lampp/lib/libz.so.1: no version information available (required by /usr/bin/python)
If i run it from the terminal window using this: php /opt/lampp/htdocs/page/index.php it works without any problems.
So any ideas how can i make it work from the browser?
Thanks
I'm guessing xampp comes with it's own libraries instead of using the system libraries, and that probably means it's setting LD_LIBRARY_PATH to it's local library directory.
This will also cause other programs started from php to use this libraries, which they may not be compatible with.
To veryfy that, try system("env");, that sould show you all the exported environment variables. If LD_LIBRARY_PATH is set, use:
shell_exec("LD_LIBRARY_PATH= /opt/lampp/htdocs/news/hello.py 2>1 &");
That unsets it before running the python script.

Calling specific version of python from PHP

I'm setting up a big system relying on python 2.7 being run through php. The call is always something like:
exec('python test.py');
However no matter what I do PHP keeps using python 2.4 for executing my files. Because of the size of the system I can't change in the programming, but will have to make 'python' point directly to python2.7.
By searching around I have reached the conclusion that I should change the php env.
echo getenv("PYTHONPATH"); // NOTHING
echo getenv("PATH"); // /bin:/usr/bin
I can do so through putenv (for example: putenv("PATH=/usr/bin/python2.7:".$_ENV["PATH"]), but php keeps running python 2.4 no matter what I change it to.
Hope somebody out there got a simple solution :)
Could you not just do this instead:
exec('/usr/bin/python2.7/python test.py');
another option, you can set path to interpreter in 1st line of script test.py
#!/usr/local/bin/python2.7
but you need make test.py executable
chmod +x path_to_file/test.py
and run from php as
exec('path_to_file/test.py');
P.S. be attentive administrators sometimes disable exec function on servers for safety.
disable_functions="popen,exec,system,passthru,proc_open,shell_exec" ....
If you can't use full path, try an alias:
alias python='/usr/bin/python2.7'
python --version
Python 2.7.2

Categories