我有一个连接到数据库的 PHP 脚本。以下数组背后的想法是将此信息插入到数据库中。该类别需要通过 ML 算法设置,为此我使用 Python 的 textblob。我正在使用 codeigniter PHP 框架。
$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'
);
Python 脚本 (machineLearning.py) 在单独调用时工作正常。我收到的错误是该类别当前保留为空字符串。我尝试使用测试:
exec("python machineLearning.py idCard", $output);
print_r($output);
但结果只是一个空数组:
Array()
Python 程序在名为 machineLearning 的函数内包含机器学习功能,并接受名为 issues 的参数。我需要传递的值
$this->input->post('issue')
进入python程序中的machineLearning函数。我是否错误地传递了参数,或者 exec() 函数是否需要正确的程序路径?谢谢大家。
找到解决方案
两个当前参数的组合已经解决了问题,我必须更新 exec() 函数内部的文件路径,并更新 exec function() 的参数部分,但必须另外使用if 语句建议。谢谢两位的帮助
看来您可能遇到错误情况,因此无法从 exec
返回有效数据。尝试将 2>&1
添加到调用和调试输出中。例如,在本地,我尝试这样做,然后得到:
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
)
您可能还想在调用中专门编码文件的绝对路径。这将解决上述“找不到文件”错误。
exec("python /path/to/machineLearning.py idCard 2>&1", $output);
我是一名优秀的程序员,十分优秀!