0

My python code gets data from php it takes value in text_content variable and then execute its script. But i dont understand to how to return back the results to php. help me please.

<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>

my python file is:

#!/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 );
2
  • You've commented out a line echo $out;, but $out does contain your data. However, it's as an array, and not a string. Is that what you saw? Just the text Array? Commented Oct 3, 2016 at 11:42
  • when i echo $out it gives me an error Array to string conversion and display only word * Array()* and if i commented out this line then i see 0 in display. Commented Oct 3, 2016 at 11:50

1 Answer 1

2

I think you have got the data, but you're tried to print it as a string, and not an array. Try:

$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;
Sign up to request clarification or add additional context in comments.

1 Comment

i want to print the result which i get from python through php. if i remove print(sent_tokenize(text_content)) and save the result in sent=sent_tokenize(text_content) and then return sent; back to php. the php couldnot display the returned results. What should i do?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.