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
Related
I am trying to compile python code using shell_exec(), or exec().
The code I am trying:
$output = shell_exec("C:/Users/AS/AppData/Local/Microsoft/WindowsApps/python.exe C:/wamp64/www/project/app/code.py 2>&1");
echo $output;
when I execute the command in shell or in the command line It works fine, However using php and (Apache sever) I get The system cannot execute the specified program. Though I can successfully execute different commands such as mkdir,
but if I try any command with .exe extension in my windows 11, I get the the same error as above.
Hi I am running shell_exec on my PHP Application,
Here is the code,
$path = "C:/scripts/";
chdir($path);
$py_commonscript = 'python Common_Script.py';
$exec = shell_exec($py_commonscript);
echo "<pre>$exec</pre>";
This code doesn't give any result and does not generate a file.
When I tried running manually on the command line it is working and was able to generate a file.
I tried to execute this one below and it was able to display some result.
$sample = shell_exec('ls -lart');
echo "<pre>$sample </pre>";
I am wondering why the command for $exec is not being triggered or run.
The most likely cause is that your PHP code isn't inheriting a %PATH containing python.exe. Try print getenv('PATH'); or just use the full path to python.exe in your command line.
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
I have tried exec() and system() command to run the python script. Nothing seems to be working.
Basically, I'm using localhost to host a webpage. In the same system, there is a python script which I want to run using PHP(from a web page through a button). I do not want python code to be called to PHP to run, I want to run PHP code in the same system using the web interface(PHP)
PYTHON CODE:
import os
from os import mkdir, makedirs
bobo = r"C:\\AmeyoS3-Automated\\bobo_folder"
os.makedirs(bobo)
PHP CODE 1(Didn't work):
system("C:\Program Files (x86)\Python37-32\python.exe C:/Users/Administrator/Desktop/ameyo-selenium/temp/bobo.py")
PHP CODE 2(Didn't work):
$python = "C:\Program Files (x86)\Python37-32\python.exe";
$file = "C:\Users\Administrator\Desktop\ameyo-selenium\temp\bobo.py";
$cmd = "$python $file";
exec("$cmd");
Python script works fine while running independently.
Problem is running the script through PHP Command.
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.