2016-10-03 90 views
0

我的Python代碼PHP中獲取數據需要在TEXT_CONTENT變量的值,然後執行它的腳本。但我不明白如何將結果返回到PHP。請幫幫我。如何從python的數據返回到PHP和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

你已經註釋掉了一行'echo $ out;',但'$ out'確實包含了你的數據。但是,它是一個數組,而不是一個字符串。那是你看到的嗎?只是文本'數組'? –

+0

當我回聲$出它給了我一個錯誤*數組字符串轉換*和只顯示文字*陣列()*如果我註釋掉這一行的話,我看* 0 *中顯示。 – jonii

回答

0

我想你已經獲得了數據,但你想打印一個字符串,而不是一個數組。試試:

$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; 
+0

我想打印從python通過php獲得的結果。如果我刪除打印(sent_tokenize(text_content))並將結果保存在sent = sent_tokenize(text_content)中,然後返回發送;回到php。該PHP無法顯示返回的結果。我該怎麼辦? – jonii