1

I have a PHP script that is connected to a database. The idea behind the following array is that this information is inserted into the database. The category needs to be set by an ML algorithm, for which I am using textblob for Python. I am using the codeigniter PHP framework.

$temp = $this->input->post('issue');
$data = array(
   'priority' => '1',
   'Summary' => $this->input->post('issue'),
   'status' => 'Submitted',
   'category' => exec("python machineLearning.py .$temp"),
   'itemsRequired' => ' ',
   'lastUpdate' => date('Y-m-d G:i:s'),
   'ownerID' => $this->session->userdata('username'),
   'managerID' => 'tech'
);

The python script (machineLearning.py) works fine when called on its own. The error I am getting is that the category is currently left as an empty string. I tried to use a test with:

exec("python machineLearning.py idCard", $output);
print_r($output);

But the result is just an empty array:

Array()

The python program has the machine learning inside of a function named machineLearning and takes in a parameter, named issue. I need to pass the value of

$this->input->post('issue')

into the machineLearning function in the python program. Am I passing the parameters incorrectly or does the exec() function require the correct path to the program? Thanks all.

SOLUTION FOUND

A combination of the two current arguments has solved the problem, I had to update my path to the file, inside of the exec() function, and update the parameter section of the exec function(), but had to additionally use the if statement suggested. Thank you both for your help

3
  • I would highly recommend escaping/sanitizing user input before using it in a shell command. Right now someone could post a malicious issue such as ; rm -Rf . Commented Dec 28, 2016 at 14:30
  • @Devon I have heard about this, but currently am working on just having this possible. For reference in futhure though, how would I go about this? Commented Dec 28, 2016 at 15:01
  • There is a function in php for this, escapeshellarg, which should come in handy. Commented Dec 28, 2016 at 17:37

2 Answers 2

1

It looks like you probably have an error condition and so aren't getting valid data returned from exec. Try adding 2>&1 to the call and debugging output. For example, locally, I try that and I get:

exec("python machineLearning.py idCard 2>&1", $output);
print_r($output);


Array
(
    [0] => /usr/bin/python: can't open file 'machineLearning.py': [Errno 2] No such file or directory
)

You might want to specifically code the absolute path to the file in your call too. This would resolve the above "file not found" error.

exec("python /path/to/machineLearning.py idCard 2>&1", $output);
Sign up to request clarification or add additional context in comments.

2 Comments

Bit of a delay in getting back, apologies for that, but can you explain the 2>&1 part please? I've looked around kind of understand, but can't see why it is necessary?
It's called redirection. Basically, i lets you capture all of the messaging that comes from your Python script instead of just STDOUT. unix.stackexchange.com/questions/99263/…
0

You need to have a "main" for your python script that reads the parameter from command line and calls your function. Something like:

if __name__ == "__main__": 
    YourFunction(sys.argv[1])

Maeby add some debuge code to print your arguments. About the path to executable, if the PATH envinronment is set corectly in your php (your server?) context, to contain the path to python, you do not need the complete path to your exe.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.