的index.php
<html>
<body>
<form action="programa.php" method="post">
Name: <input type="text" name="longitud"><br>
E-mail: <input type="text" name="angulo"><br>
<input type="submit" value="correr">
</form>
programa.php
<?php $longitud=$_POST["longitud"];
$angulo=$_POST["angulo"]; ?>
<h1>Corriendo programa con <?php echo $_POST["longitud"]; ?> mm de longitud<br>
y <?php echo $_POST["angulo"]; ?> grados de angulo</h1>
<br>
<br>
<br>
<?php
$a=exec('sudo python /var/www/python/programa.py $longitud $angulo');
echo $a;
?>
我访问了错误日志并打印了这个
Traceback (most recent call last):
File "/var/www/python/programa.py", line 7, in <module>
script,long_barra,angulo = argv
ValueError: need more than 1 value to unpack
[Mon Nov 10 22:36:53 2014] [error] [client 192.168.1.3] PHP Notice: Undefined
index: longitud in /var/www/Maquina/programa.php on line 22
[Mon Nov 10 22:36:53 2014] [error] [client 192.168.1.3] PHP Notice: Undefined index:
angulo in /var/www/Maquina/programa.php on line 24
[Mon Nov 10 22:36:53 2014] [error] [client 192.168.1.3] PHP Notice: Undefined index:
longitud in /var/www/Maquina/programa.php on line 25
[Mon Nov 10 22:36:53 2014] [error] [client 192.168.1.3] PHP Notice: Undefined index:
angulo in /var/www/Maquina/programa.php on line 26
Traceback (most recent call last):
File "/var/www/python/programa.py", line 7, in <module>
script,long_barra,angulo = argv
ValueError: need more than 1 value to unpack
Traceback (most recent call last):
File "/var/www/python/programa.py", line 7, in <module>
script,long_barra,angulo = argv
ValueError: need more than 1 value to unpack
programa.php上的脚本读取了post值,但没有将argv传递给python脚本,我该如何解决呢?
提前
答案 0 :(得分:0)
您的参数未传递给python的原因是exec()
调用中的单引号。这会向python发送文字字符串“$ longitudinal”和“$ angulo”,而不是所述变量的值。相反,使用双引号,PHP将用它们的值替换变量。
因此,请将exec()
电话改为:
$a=exec("sudo python /var/www/python/programa.py {$longitud} {$angulo}");
// ^ notice the double quotes ^
在这种情况下,花括号不是必需的,但为了便于阅读,我建议使用它们。
或者,您可以直接从$_POST
访问这些值,而无需将值分配给变量,如下所示:
$a=exec("sudo python /var/www/python/programa.py {$_POST['longitud']} {$_POST['angulo']}");