Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Can you use both languages to build a website? A better, and more accurate question is this: Can you build the website in PHP, and use Python to execute other tasks? For example, maybe Python has a great, very particular library for accomplishing some task. Is there a standard protocol for communicating between PHP and Python?

A common use case may be that a bunch of data is collected through the PHP website (maybe stored in a MySQL database), passed to Python for some analysis, and then the Python returns it to the PHP (by perhaps storing it in a different location in the MySQL database) so the new analysis can be displayed on the website by the PHP. Any advice/pointers/references would be great.

share|improve this question
1  
Yes............ – Asad Apr 11 at 23:34
One architecture is to have PHP/Apache handle the web requests and the "back end" tasks (which can be in any language) run periodically. Most Linux servers include a utility called "cron". Cron will run a program or script every NN minutes, or at specific times of day, once a week on Saturday night, etc. You don't need a channel directly between PHP and python or other software if they all have access to a MySQL database they can all just use that. – Paul Apr 11 at 23:39
I didn't think this question was much a of debate. Paul Hulett and doublesharp gave great answers and provided what I was looking for. Thanks, guys! – user1114864 Apr 12 at 18:03

closed as not constructive by nickb, Pavel Anossov, Asad, Chris Cooney, Makoto Apr 11 at 23:37

As it currently stands, this question is not a good fit for our Q&A; format. We expect answers to be supported by facts, references, or specific expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, see the FAQ for guidance.

2 Answers

up vote 2 down vote accepted

You can't make function calls from PHP to python, but you could certainly have data saved in the database and then processed in python and then read the results in PHP, or pass in parameters to a python script.

// save something to the db and access it from python, or elsewhere
$db->save();

$arg = "argument";

// execute a python script and get the result
$result = exec("python script.py $arg");

// do something else
echo $result;

If the work can be done asynchronously, you can have your website written in PHP and then do batch processing of your data in python, or vice versa.

share|improve this answer

You can use any of the execution schemes found here to execute a python file (or anything else that the system can run) from this list: http://us3.php.net/manual/en/ref.exec.php

I've often used exec(), but there are the usual issues with security that have to be dealt with.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.