Executing Ruby script from PHP and getting output - php

I have this Ruby script (test.rb):
print "hello"
And I have this PHP script (test.php):
$cmd = "ruby test.rb";
system($cmd);
Now I call my PHP script from CLI this way:
php test.php
And I get no output (it should print "hello")
Why?

system would capture the output of the ruby script.
you might want to do:
$cmd = "ruby test.rb";
echo system($cmd);

Its working for me, check whether both ruby script and php in the same folder and installed ruby in your machine
<?php
$cmd = "ruby test.rb";
system($cmd);
?>

Related

Call python script with php (windows xampp server)

Trying a very simple task. Call python example.py from a php script from a windows xampp server.
Notes:
- Both files are in the same directory.
- Going to http://example.com/example.py works fine.
- Going throught the command line (cd inside where example.py is then (python example.py)) works also.
example.py
#! C:{path-to-python.exe}
print("Content-Type: text/html\n")
print("<html>")
print("<h1>Something.</h1>")
index.php
<?php
$command = escapeshellcmd('python example.py');
$output = shell_exec($command);
echo $output;
?>
Nother note:
- If I use $command = escapeshellcmd('example.py'); it opens the python script in my text editor.
Someone tell me plz what am I doing wrong. Thank you.
to call python script from php :
$result=exec("python test.py ");
if you want to passing parameters to python script :
$result=exec("python test.py param1 param2");
in python script if you want get parameters:
import sys
param1=sys.argv[1]
param2=sys.argv[2]
note : $result in php taking first print statement in python script

Running a python script with php on a web server

I've got my website hosted at one.com and I'd like to run a simple python script with php that returns 'hello' but I get no result.
It worked perfectly on local with Mamp.
What should I configure for it to work online ?
My php code:
<?php
$result = shell_exec('python test.py');
echo $result;
?>
The python script
print('hello')
First of all make sure that the python script is executable on the server. For safety reasons I would remove the space in the filename as well so make it just test.py for now. You can make it executable by simple changing the permissions using chmod +x test.py
Then, you can try the following lines of code in php
<?php
$command = escapeshellcmd('/usr/custom/test.py');
$output = shell_exec($command);
echo $output;
?>

Execute python program using php on server

I have a python program that i want to execute from php I already tried "exec,escapeshellcmd,shell_exec,passthru,popen" but no one is giving result.
i already execute "hello word" program successfully but the program that i needed is not executing and not displaying any error.
echo $python = exec("python python/interactive.py");//this file not working
echo $python = passthru("python python/python.py ");// this is working with hello world.
$output = shell_exec($command);
echo $output;
I suggest that you look into a python web framework like Flask or Django which are the two main ones if you want to be executing python scripts on the web.This would be the best way to do it.

Execution of a Python Script from PHP

I am making an android application in which I am first uploading the image to the server and on the server side, I want to execute a Python script from PHP. But I am not getting any output. When I access the Python script from the command prompt and run python TestCode.py it runs successfully and gives the desired output. I'm running Python script from PHP using the following command:
$result = exec('/usr/bin/python /var/www/html/Source/TestCode.py');
echo $result
However, if I run a simple Python program from PHP it works.
PHP has the permissions to access and execute the file.
Is there something which I am missing here?
exec('/usr/bin/python /var/www/html/Source/TestCode.py', $output);
var_dump($output);
2nd Parameter of exec will give output
EDIT:
exec('/usr/bin/python /var/www/html/Source/TestCode.py 2>&1', $output);
2>&1 - redirecting stderr to stdout. Now in case of any error too, $output will be populated.
First Check your python PATH using "which python" command and check result is /usr/bin/python.
Check your "TestCode.py" if you have written #!/usr/bin/sh than replace it with #!/usr/bin/bash.
Than run these commands
exec('/usr/bin/python /var/www/html/Source/TestCode.py', $result);
echo $result

error on using exec() to call python script in browser

I am trying to call a python script by php:
<?php
$a = exec("python ~/www/encrypt.py");
echo $a;
?>
I use terminal and php index.php command to see result and it works well but in a web browser it does not show anything.
Problem solved by full path >> exec("python /var/www/encrypt.py");

Categories