i'm using an API, which creates a JSON file when the python script is executed, so i've tried to use exec to run the python script and thne retrieve the json. However the python script does not seem to execute. What am i doing wrong? i'm trying it on a apache using MAMP
exec('python http://localhost:8888/examples/recent_matches_to_json.py');
$json = file_get_contents('http://localhost:8888/examples/recent_matches.json');
$obj = json_decode($json);
var_dump($obj->recent, true);
Uh... You should download the script and save it, then run the interpreter on it.
Something like:
$py_script = file_get_contents('http://localhost:8888/examples/recent_matches_to_json.py');
file_put_contents('recent_matches_to_json.py', $py_script);
exec('python recent_matches_to_json.py');
If you intend to download the script from the local computer, why not run it directly? Like exec('python /home/peter/site/examples/recent_matches_to_json.py').
You could also set up a web/WSGI server in that Python script so you could run it directly by just sending a HTTP request (like http://localhost:9999/recent_matches_to_json/).
Related
running process in background on hangs PHP page
I did as explained in this above link but it did not work.
Question:
I want to send mail using Python from PHP. When I call Python, my page waits for a response from Python. I want to run Python in the background.
I also redirect Python's response to another location ex: "E:\\nil.txt", but it still waits for a response.
I am working on Windows 7. My problem is that all the answers I could find were for Linux.
My PHP code:
public static function callPython($scriptName, $data){
$path = "C:\\Users\\Aditya\\Desktop\\".$scriptName.".py";
//$data=array("to"=>$to,"name"=>$name,"headingSection"=>$headingSection,"body"=>$body,"subject"=>$subject);
$jsonArray = json_encode($data);
$jsonArray = str_replace('"', '\"', $jsonArray);
$dir= "E:\\nil.txt";
$cmd = 'python '.$path.' '.$jsonArray.' >'.$dir.' 2>&1 &';
var_dump($cmd);
shell_exec($cmd);
}
If you need my Python script, then I will post it here.
the main thing is how the python file is getting executed from the php code you have written? it is only loading the file in the $path file. and how are you getting the $data while it is not having anything as you are not storing any.
hope you get my point.
i will give you an idea.
When the php file is calling the python file. the python file should execute like we do in cmd. python some.py like this. then whatever is returned should be loaded in your php function to see it.
hope it helps
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
I am using XAMPP on Windows 7 to run a localhost, and I'm trying to use the PHP exec function to execute a Node.js script.
I am able to successfully use the exec function to run PhantomJS scripts, but when I try to do the same thing for Node.js, it doesn't work.
He's an example of a PHP script that properly runs a PhantomJS script:
<?php
exec('/phantomjs/bin/phantomjs /phantomjs/scripts/test.js', $output);
print_r($output);
And here's a similar example of a Node.js script that outputs an empty array every time:
<?php
exec('/Program Files (x86)/nodejs/node /Program Files (x86)/nodejs/test.js', $output);
print_r($output);
I'm sure that all of my paths are correct and that the Node.js script executes correctly whenever I execute it directly from the command line, but I can't get anything to return from the Node.js script when I run it from the PHP exec command. I also tried the following script, but I still get nothing:
<?php
exec('/Program Files (x86)/nodejs/node -v', $output);
print_r($output);
Any advice on what I'm missing would be greatly appreciated. Thank you.
i want to execute a python script on my server which execute and return a list of data.
How can i execute python script by php and return back the python list data in php code.
for example.
python_file.py
import os
def list_data():
"""
this function will return a content list of path directory
:return:
"""
path = 'C:/'
my_list = os.listdir(path)
return my_list
print list_data()
use this:
<?php
$data_list = `python path/to/python_file.py`;
?>
With exec command you can execute a shell command.
http://php.net/manual/en/function.exec.php
So basically any script ( including python ) can be executed.
But using exec is not encouraged in web apps. It basically lets you run shell commands and execute them on the server. Most shared hosts come with it disabled.
I have one python script for handling outlook emails using Outlook MAPI. I have to call this script from PHP code. If I call script from cmd it is fine, but when I do it from PHP doesn't work and there are no errors. Also if I just call some other python script with simple file writing code from same PHP script it works. I have no idea what to do. I am trying with exec('python c:/xampp/SCRIPT_LOCATION/SCRIPT.py') as well as shell_exec(), system()... but with no progression.
Try this:
$python = 'C:\\Python27\\python.exe';
$pyscript = 'C:\\xampp\\SCRIPT_LOCATION\\SCRIPT.py';
exec("$python $pyscript");