Calling a Python script from PHP does not work - php

I'm creating a webpage in PHP wherein if a button is pressed, the PHP script executes a python script using shell_exec() command. The python script creates a pdf file using fpdf python library. When I run the same from command shell, it works prefectly but when I try to run python script from PHP, it gives me nothing. Here is PHP code:
<!doctype html>
<?php
$command = escapeshellcmd('/home/amogh/server/test.py');
$output = shell_exec($command);
echo $output;
?>
Python script:
#! /usr/bin/python
#print ('hello')
from fpdf import FPDF
fp = open('downloads/boot.txt', 'r')
pdf = FPDF()
pdf.add_page()
pdf.set_font('Arial', '', 11)
line = fp.read()
pdf.multi_cell(200, 5, line, 0, 1)
pdf.output('test.pdf', 'F')
Also, I'm using lighttpd server. Can anyone tell me where am I going wrong?

I'd guess the working directory is wrong and your script can't access the file it needs.
You may want to use
shell_exec("/home/amogh/server/test.py 2>&1");
in order to redirect stderr to stdout, so any error output from Python will be also in the $output variable, to help you debug things.

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

How to fix no output with python linked to php?

I have a python script which takes data from a csv file via pandas. When I run this solely this works fine. But when I run it within php it does not work. I want it to work like that so I can output the python output onto the website.
Python code:
#!/usr/bin/env python
import pandas as pd
teams = pd.read_csv("/var/www/html/teams.csv")
print(str(teams['punt'][0]))
PHP code:
<?php
$command = escapeshellcmd('python3 /var/www/html/python.py');
$output = shell_exec($command);
echo $output;
?>
Under the PHP is some irrelevant html code. It would be very helpful if someone could help me!
EDIT:
When I run the php in terminal it does not give any errors and it gives the result perfectly. But when I run localhost it does not work but I cannot seem to find any errors. I also tried running a different python script which only prints "hello" and when I try that the code does work and the text shows up in the browser.

Unable to execute Python script from PHP shell_exec

I'm trying to execute a python (Python 3) script from the PHP shell_execfunction but it doesn't seem to be working, I'm trying it on a windows system but it will be in CentOS 7 in production.
In the below code snippet nothing happens when the PHP runs. But it I use the $response = shell_exec("php -v"); line it displays the PHP version ok. I've also tried running the python script directly on the command line and it runs fine with no errors.
Code
$command = "\"C:\Programs\Python\Python36-32\python.exe\" \"E:\\htdocs\\dev\\_nodes\\jobs\\jobCheck.py\" \"https:/www.google.ie\" 1";
$response = shell_exec($command);
//$response = shell_exec("php -v");
echo $response;
Command Line
"C:\Programs\Python\Python36-32\python.exe" "E:\\htdocs\\dev\\_nodes\\jobs\\jobCheck.py" "https:/www.google.ie" 1
Update #1
I've gotten the python script to run via PHP, but the python script crashes once it hits driver = webdriver.PhantomJS(), the import exists in the python script from selenium import webdriver and all this functionality works fine once called directly via the command line but doesn't seem to work once the python script is called from PHP.
Here's a sample script that prints out test 1 but not test 2 when called from the PHP script.
from selenium import webdriver
print("test 1")
driver = webdriver.PhantomJS()
print("test 2")
driver.quit()
Update #2
Looks like it was an issue with paths, there is a file created in the python script that was using a relative path, once I changed the path to be a full path the script ran ok both in the command line and when being called via the PHP script.
You need to scape the command string.
Try using escapeshellarg function.
$command = escapeshellarg('C:\Programs\Python\Python36-32\python.exe "E:\htdocs\dev\_nodes\jobs/jobCheck.py" "https:/www.google.ie" 1');
$response = shell_exec($command);
//$response = shell_exec("php -v");
echo $response;
http://php.net/manual/pt_BR/function.escapeshellarg.php

Running python script from php and write to a file

My php code is in htdocs/series/index.php.
My python code location is /home/shivam/Desktop/hello.py
I am running php using xampp in ubuntu.
here is php code
<?php
$command = escapeshellcmd('/home/shivam/Desktop/hello.py');
$output = shell_exec($command);
echo $output;
?>
here is my python code
#!/usr/bin/env python
with open("/home/shivam/Desktop/solution.txt", "w") as myfile:
myfile.write("write text")
I am trying to run the python script using php to write to a file 'solution.txt'. When i write something myself to solution.txt and open it in read mode using python script via php then it works but when i try to write using above code ,i am unable to do so.
One more thing, none of the code below the line "with open("/home/shivam/Desktop/solution.txt", "w") as myfile:" gets executed.
I have tried the solution given in : enter link description here
Absolute paths to executables and files...
run_py.php
<?php
$command = escapeshellcmd('/usr/bin/python /home/user/some_py.py');
$output = shell_exec($command);
echo $output;
?>
some_py.py
with open("/home/user/solution.txt", "w") as myfile:
myfile.write("write text")
Output:
user#box:~/$ php run_py.php
user#box:~/$ cat solution.txt
write text

php can run the python programm from php shell but not on the webbrowser

I have a python script that uses ipyhon and it can be executed when I run it from the terminal using php -a (interactive shell), but when I type localserver/RDMIP_KE.php (test.php is the script wrtten with gedit in ubuntu) nothing happens and I only see a blank page. Any idea why?
<?php
$command = escapeshellcmd('/home/administrator/Desktop/RDMIP_KE.py');
$output = shell_exec($command);
echo $output;
?>
Just an update: I checked the log file and it gives me a list of errors for example about the pyplot: File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2460,ax = gca() in plot
But again as I said before the python program works fine and can be executed with php if I run it from the php shell. I don't understand what's going on.

Categories