i am finding problem in passing string from php to python code.i think my code is fine but may be in problem of path of python file.so suggest me where i have to place python file when it execute with php file.thank u so much in advance
here is my code:-
test.php
<?php
$s= "what is your name";
$output= shell_exec('xampp/htdocs/fbchat/public/test.py ' .$s);
echo $output;
?>
test.py
import sys
x= sys.argv[1]
print x
You can place your python script any where, but you need to provide its full path name(exact location).
Assuming your file is in C drive of your computer:
Try this:
shell_exec('python c://xampp/htdocs/fbchat/public/test.py ' . $s);
Try this:
shell_exec('python xampp\\htdocs\\fbchat\\public\\test.py ' . $s);
Related
i have a php file which have some variables para1 and para2 , i want to send them to a python file .
this is my php file:
<?php
$para1 = "one";
$para2 = "two";
echo "shell_exec("
/C:/xampp/htdocs/Ai_Edutech_trial_project/eclipse_workspace/Project
/check.py '$para1' '$para2'")";
?>
and this is my py file:
import sys
x=sys.argv[1]
y=sys.argv[2]
print(x)
print(y)
but this does not work for me. Can someone please help me with this or suggest some other way?
Don't put quotes around the function call. That turns it into a literal string, not a call to the function. Windows pathnames don't use a / before the drive letter, so the path should start with C:. And there shouldn't be a space after Project.
echo shell_exec("C:/xampp/htdocs/Ai_Edutech_trial_project/eclipse_workspace/Project/check.py '$para1' '$para2'");
Also, if you're just going to echo the result, you can use the passthru function instead.
I had a similar problem. Solved it by adding the word python in front of the path to the python script.
As in:
echo shell_exec("python C:/xampp/htdocs/Ai_Edutech_trial_project/eclipse_workspace/Project/check.py '$para1' '$para2'");
I am trying to call a python script from php. However, I cannot figure out how to return the result to my php script in order to use it there. My php script is as follows:
<?php
$input = 'help';
$output = passthru("/usr/bin/python3.5 /path/test.py '$input'");
echo $output;
?>
whilst my python script 'test.py' is:
import sys
json2 = sys.argv[1]
That's because your python script returns nothing. Just doing:
json2 = sys.argv[1]
Only assigns the value to json2. I suspect you're trying to do something like:
import sys
json2 = sys.argv[1]
print json2
That seems to work just fine:
~ # php -a
Interactive shell
php > $input = 'help';
php > $output = passthru("/usr/bin/python test.py '$input'");
help
Update:
Based on your comment. You're looking for exec() instead:
~ # php -a
Interactive shell
php > $input = 'help';
php > $output = exec("/usr/bin/python test.py '$input'");
php > echo $output;
help
Note that your Python script has to output something. Just assigning a variable is not going to work, since PHP cannot interpret your Python code for you. It can only run the script and do something with the script's output.
Here is my code, that is supposed to work according to answers to other similar questions, but it does not.
PHP:
<?PHP
$par = $_POST["parameter"];
$importPar=exec("py pyData.py . $par"); //also tried shell_exec()
print ($par);
print($importPar);
?>
Python:
import sys
who = sys.argv[1]
print("This is php var: ",who)
It might have to do something with my cmd because nothing is returned even when I try:
$test=shell_exec('ipconfig');
echo $test;
With minor modifications, your code works just fine on my PHP 5.6.3 setup. The only substantive difference in my example below is that $par gets set locally, and not from a $POST input.
Minor corrections:
1. The . isn't necessary when you're including a $variable inside double quotes. This will actually make python think you want to print . instead of $par, as . is read as sys.argv[1].
2. I'm assuming py is an alias, but I needed to use python in exec() to run properly.
3. You'll get a tuple as print output with your python statement as-is: ('This is php var: ', 'foo')
. Consider using .format() instead, see below.
""" test.py
import sys
who = sys.argv[1]
print('This is php var: {}'.format(who))
"""
<?PHP
$par = "foo";
$importPar=exec("python test.py $par");
print ("$par\n");
print($importPar);
?>
Output:
foo
This is php var: test.py
Process finished with exit code 0
I Have been stuck for a couple of days now. I am attempting to call a simple python script from PHP. For the life of me I cannot figure out what the issue is. I have made the onoff.py script executable with chmod +x.
I can run the script just fine from the command line like this:
pi#raspberrypi:/var/www/html $ python onoff.py
LED on
LED off
My issue is when I try to call the script from PHP. I get nothing.
My Python Script:
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18,GPIO.OUT)
print "LED on"
GPIO.output(18,GPIO.HIGH)
time.sleep(1)
print "LED off"
GPIO.output(18,GPIO.LOW)
My PHP Script:
<?php
$command = escapeshellcmd('python /var/www/html/onoff.py');
$output = shell_exec($command);
echo $output;
?>
Any help is greatly appreciated!
EDIT:
if I change my onoff.py script to a simple while loop such as:
#!/usr/bin/python
x=1
while (x<10):
print x
x=x+1
the output on the browser is:
1 2 3 4 5 6 7 8 9
I just don't understand why the loop will run but I get no output with the original python code.
EDIT 2:
Ok So I taking a different approach and trying to see where the code fails. I am adding bits of code at a time. Please see the following.
#!/usr/bin/python
import random
import time
import RPi.GPIO as GPIO
randomNumber = random.randint(1, 20)
GPIO.setmode(GPIO.BCM)
#GPIO.setup(18,GPIO.OUT)
print randomNumber
Now when I run the PHP it shows a random number so I know the python script is running. When I un-comment GPIO.setup(18,GPIO.OUT) and run the php I get a blank screen. I have no idea why this would make the script fail.
shell_exec() will only return a string if $command a). Ran OK and assuming b). that it spits its response to STDOUT.
Use exec() and pass it your command, an integer $code and an empty array $response both of which are treated by PHP as arguments by reference.
Run your command thus:
$command = escapeshellcmd('/path/to/python /var/www/html/onoff.py');
$response = array();
$code = 0;
exec($command, $response, $code);
var_dump($code, $response);
die;
You should now see what is actually being given to PHP internally and why the Python script isn't working.
You need to use python before the script, i.e.:
$command = escapeshellcmd('python /var/www/html/onoff.py');
If it doesn't work, python probably ins't on the PATH of the apache user and you may need to use the full path to the python binary, use which to find the full path:
which python
//usr/bin/python
The use that value:
$command = escapeshellcmd('/usr/bin/python /var/www/html/onoff.py');
Note:
Make sure apache user has execute permissions on onoff.py
I have installed SymPi in the server and from the command line, I am able to execute the following.
python ./sympy-0.7.5/bin/isympy
(this will open a console where I can type mathematical expressions. then the following expression)
1 + 2
(will give 3 as output)
My aim is to do the same from php using shell_exec. I have written a php file as given below, but is not working.
$command = escapeshellcmd('python ./sympy-0.7.5/bin/isympy');
shell_exec($command);
$output = shell_exec('1 + 2');
Can anybody help me to figure out why this is not working?
Please note that the following script works fine which just execute a python script and retrieve the output.
$command = escapeshellcmd('python C:\PythonPrograms\test3.py');
$output = shell_exec($command);
echo $output;
My guess is that the working directory (cwd) of shell_exec is different from the one you're in when you execute it manually.
Your working example specifies a hard path that will work from anywhere. Whereas your not-working example specifies a relative path (./ is the cwd).
Convert your call to isympy to give its full path on disk. Or figure out how to set the cwd of shell_exec.
(If this doesn't solve it, say more than "is not working." What happens? An error? What is the full text of the error?)
Each time you run shell_exec, it opens a completely new instance of the shell.
Edit:
You can pass a command for python to execute like this:
$expression = '1 + 2';
$cmd = 'python -c \'print "%f" % (' . $expression . ')\'';
$output = shell_exec($cmd);
This, admittedly is not using sympy, but for simple mathmatical expressions you may not need to. If you do, you would just need to import the library in the same command, like this: python -c 'import sympy; print "%f" % sympy.sqrt(3)'
I could manage the desired result in a different way.
Created a python script which accepts the expression as the command line argument , execute and display the output.
Call this script from php by passing the expression as the command line argument.