我的python代码从php获取数据,它在text_content变量中获取值,然后执行其脚本。但我不明白如何将结果返回到PHP。请帮帮我。
<body>
<?php
// define variables and set to empty values
$text_content="";
$hello="";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$hello = $_POST['text_content'];
$command="\Users\jonii\AppData\Local\Programs\Python\Python35\python splitter.py $hello ";
exec($command , $out,$ret );
//echo $out;
echo $ret;
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<textarea name="text_content" value="<?php echo $text_content;?>" cols="40" rows="4"> </textarea>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
我的python文件是:
#!/Users/jonii/AppData/Local/Programs/Python/Python35/python
# Import modules for CGI handling
import cgi, cgitb
import nltk
from nltk.tokenize import sent_tokenize, word_tokenize
import sys
print("Content-type:text/html\n")
print("hello")
text_content = ''
for word in sys.argv[1:]:
text_content += word + ' '
print(text_content)
def sentence_split( text_content ):
# Add both the parameters and return them."
print(sent_tokenize(text_content))
return
# Now you can call sentence_split function
sentence_split( text_content );
答案 0 :(得分:2)
我认为您已获得数据,但您尝试将其打印为字符串,而不是数组。尝试:
$command="\Users\jonii\AppData\Local\Programs\Python\Python35\python splitter.py $hello ";
exec($command , $out,$ret );
//echo $out;
/*Loop through each line of data returned*/
foreach ($out as $line){
print "$line\n";
}
echo $ret;