1

I have a great Python program on my webserver, which I want to use from inside my PHP web app.

Here's an example of the python command, and output as you would see it in terminal:

>>> print MBSP.parse('I ate pizza with a fork.')
I/PRP/I-NP/O/NP-SBJ-1/O/i
ate/VBD/I-VP/O/VP-1/A1/eat
pizza/NN/I-NP/O/NP-OBJ-1/O/pizza
with/IN/I-PP/B-PNP/O/P1/with
a/DT/I-NP/I-PNP/O/P1/a
fork/NN/I-NP/I-PNP/O/P1/fork ././O/O/O/O/. 

You might recognize this as a typical POS tagger.

In any case, I'm confused about how to use a PHP-based web app to send this program a string like "I ate pizza with a fork", and somehow get the response back in a way that can be further parsed in PHP.

The idea is to use PHP to pass this text to the Python program, and then grab the response to be parsed by PHP by selecting certain types of words.

It seems like in PHP the usual suspects are popen() and proc_open(), but popen() is only for sending, or receiving information - not both? Is popen() able to give me access to this output (above) that I'm getting from the Python program? Or is there a better method? What about curl?

Here are all my options in terms of functions in PHP: http://us.php.net/manual/en/function.proc-open.php

I'm lost on this, so thanks for your wise words of wisdom!

asked Feb 20, 2011 at 23:38
1
  • yeah - that's the challenge I'm having! What is better - Exec() or something like popen() or proc_open()? Commented Feb 21, 2011 at 7:16

5 Answers 5

2

I use exec() for this purpose.

exec($command, $output);
print_r($output);
answered Feb 20, 2011 at 23:46
Sign up to request clarification or add additional context in comments.

Comments

2

If you want to get a little heavier / fancier... give your python script an http (or xmlrpc) front end, and call that with a GET/POST. Might not be worth all that machinery though!

answered Feb 21, 2011 at 0:27

3 Comments

xmlrpc I think is definitely the way to go! check this out docs.python.org/library/simplexmlrpcserver.html it gets even easier when you are using a python web dev framework...
I suspect they might not want to fire up a python web server, considering the main app is in PHP :)
Yeah - this is a great solution, but for simplicity's sake, I would rather focus on popen() or proc_open() or exec() ??? Three options is too redundant for me - which is best for a simple app?
1

You could use popen(), and pass the input to your Python script as a command line argument, then read the output from the file descriptor popen gives you, or proc_open() if you want to interact bi-directionally with the Python script.

Example 1 in the proc_open manual: http://us.php.net/manual/en/function.proc-open.php gives an example of this.

answered Feb 20, 2011 at 23:47

1 Comment

I've read that popen() is faster and easier to use than proc_open(), is that true?
0

If your Python needs it as stdin, you could try popening a command line:

echo "I ate pizza!"|my_python_progam.py

and just read the output. As usual, do proper input validation before sending it to the command-line.

Martijn Pieters
1.1m326 gold badges4.2k silver badges3.5k bronze badges
answered Feb 27, 2011 at 1:25

Comments

0

Something like this would work

 $command = '/usr/bin/python2.7 /home/a4337/Desktop/script.py'
 $pid = popen('$command',r)
 ........
 ........
 .........
 pclose($pid)
answered Dec 12, 2014 at 17:29

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.