call python from php [duplicate] - php

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Call Python From PHP And Get Return Code
probably a very stupid question
I have a python module lets say hi.py which have the following
print "hi"
now I want to execute this file from php
<?php
#code here
?>
So how do I call that python file from php.
Thanks

Use the exec function to invoke Python.
Example:
$output = array();
exec("python hi.py", $output);
var_dump( $output);
There are other commands for executing commands on the machine PHP is running on, see the docs.

you can call php exec function and call
python -c "print 'hi'"
or if you have larger module you can use
-m mod : run library module as a script (terminates option list)
so in the end I'd use
exec("python -m hi");
and don't forget to configure PYTHON_PATH on your server if needed
be careful doing this kind of stuff

Related

How can I run php commands directly in console? [duplicate]

This question already has answers here:
How can I execute PHP code from the command line?
(6 answers)
Closed 2 years ago.
When I run PHP in my console (any cli application) and I sending a command like below, it not returned anything!
echo 'Hi!';
Is there any way to run PHP commands directly in console or just we can run a .php file?
Note: My environment variables and path is OK.
You may use PHP's interactive shell
php -a
echo 'Hi!';

How to run a .sh file with arguments using shell_exec in PHP? [duplicate]

This question already has answers here:
How can I debug exec() problems?
(4 answers)
Closed 3 years ago.
attempting to run a shell script using PHP. the shell script contains:
'cd /data/nfsshare; python /data/nfsshare/kron_hull.py> /data/nfsshare/log.txt'
that is running a python script named kron_hull.py
the php code im using is:
if ($_POST['visual']) {
shell_exec("sh test.sh");
echo "working! woot woot";
} else {
echo "not working";
}
i am using the echo's as a way to tell if its running the if statement correctly, and i am able to successfully echo "working! woot woot", but the shell file does not run, because the script is meant to create a new file on the server, which it does not.
the shell file works when you put it directly in the linux terminal as "sh test.sh" so the issue is not with the shell file.
i also ran shell_exec('whoami'); and shell_exec('ls -lart'); and was able to echo both of those, so i don't believe it is a problem with the shell_exec() command.
my problem is: i dont believe the shell script is even running when put into the shell_exec() command, and I dont know a better way to check besides looking for the new file its supposed to create, which i get none.
any help is appreciated, if you need me to do anything for testing, or need more information, lmk and ill be glad to answer.
i checked the related questions and they did not help my case
One way to check whether the shell command executed correctly is to use the exec() function instead. exec() gives you the ability to see the return value and the output in a variable.
You can build a error checking execution like this:
if ($_POST['visual']) {
exec("sh test.sh", $output, $return);
// Return will return non-zero upon an error
if (!$return) {
echo "working! woot woot";
} else {
echo "Error with shell script";
}
}
To see error codes you can look here.

How can I run a linux command from a PHP script [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
php shell_exec() vs exec()
How do I run a linux command from a PHP script? I'm running Linux Debian and PHP5. I want to be able to issue a wget command to the console.
An example of what I'm looking for is something like this:
phpFunction ("wget http://www.example.com/image.jpg /folder");
echo "done";
Also would I be able to echo the output of that function?
Use exec to run any command. Be careful not to exec any user input though, as it can severely compromise your server.
Also, note that most shared servers block off the exec function so you won't be able to use it.
Finally, as a shorthand, you can wrap the command you want to exec in backticks.
You can do what you want with the following code :
system(command);
See http://php.net/system
You can execute linux commands within a php script - all you have to do is put the command line in brackits (`).
And also concentrate on exec() , this and shell_exec() ..
you can use
exec
shell_exec
http://php.net/manual/en/function.shell-exec.php

php exec problems [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Call another PHP script and return control to user before the other script completes
How to run a PHP script asynchronously from a another PHP script?
I have some problems on using exec() in PHP but I have no idea to solve it.
I want to use exec() to execute another php script in same directory 'asynchronously'.
I open a.php in browser to call b.php but it doesn't work. (the localhost is xampp on windows 7)
a.php:
<?php
exec('php \b.php', $output, $r);
print_r($output);
print_r($r);
?>
b.php:
#!/usr/bin/php
<?php
echo time();
?>
The output in browser
Array ( ) 1
I am super beginner on using something related PHP CLI... I have no idea about this...
Can anyone give some simple examples? For example, what should be written in a.php and b.php.
Thanks a lot if you can give me some guides or advice!
EDIT:
I tried following code and it works but it does not run 'asynchronously'... How can I redirect the output?
Open callexec.php in browser.
callexec.php
<?php
exec('C:\xampp\php\php.exe testexec.php');
?>
testexec.php
<?php
echo "start: ",time(),"\n";
sleep(10);
echo "\n";
echo "end: ",time(),"\n";
?>
Thanks again.
As you are already telling your shell-script that it is a php script with #!/usr/bin/php, you don't need to invoke php for your script, you can just call it like any script from the command line:
exec('b.php', $output, $r);
By the way, I am assuming that you can call b.php from the command line without any problems.
Why make things complicated? Just use include or require?
There might be a problem with port numbers if you have another server on the same platform. For instance it could be an oracle database server, mysql, Apache tomcat, etc. Please confirm whether the port numbers of the different servers are the same.

Python and executing php files? [duplicate]

This question already has an answer here:
Closed 12 years ago.
Possible Duplicate:
Start local PHP script w/ local Python script
How do you execute a php file and then view the output of it?
os.system("php ./index.php")
Does not work only returns 0
What you need is the os.popen function. It runs the command and returns the stdout pipe for that commands output. You can also capture the stdin, stderr by using os.popen2, popen3
import os
outp = os.popen('php ./index.php')
text = outp.read() # this will block until php finishes
# and returns with all output in text
print text
If you're working under Windows, you can use os.startfile().

Categories