Home > Article > Backend Development > Php calls python module_PHP tutorial
I was once asked during an interview: How to use PHP to call external programs?
Maybe it’s because I haven’t used it in my usual projects, or maybe I’m just lazy. I was dumbfounded for a long time and didn’t answer the interviewer’s question because I thought it was familiar. Yet it is paradoxical. After that interview, I found a lot of information to study, and finally understood the answers to the interviewer's questions. Here, I will also share my learning results with you.
(Digression: In fact, sometimes it is necessary to go to more interviews. Even if you don’t get the job, it is a gain to let the interviewer’s questions check and fill in your gaps.)
Let’s get to the point:
Environment: windows XP, php5.3, python2.6.6
1.Install python (http://www.python.org/ftp/python/2.6.6/python-2.6.6.msi)
2. Add the directory where python.exe is located to the system variable
Mine is F:Python26
If the configuration is successful, In DOS mode type python Will enterpythoncommand line mode:
3.php installation (php installation will not be explained in detail. If you don’t know how, please go to Baidu of);
4.Write a test python script File name: php_get_python.py
5.Use php to call the python module, php_get_python.php
6.Try running the result:
How? Very simple, right?
1. The python module in this article is relatively simple, you can spread your thinking, python can be used to make spider crawlers, calculate ... and other operations.
2. Understand and master system variables well, and you will have unexpected gains.
If you add E:wampwwwpython_module to the system variable
Steps 5 can be simplified to
system('php_get_python.py');
3. This calling mode is also valid for EXE file, java module , you only need to change the execution content of the 5 step system() function.
Welcome to my personal blog: http://hi.baidu.com/anran89757