Home > Article > Backend Development > How to run Python script in PHP program
(Recommended tutorial: PHP video tutorial)
Introduce how to run Python scripts in php programs,
The running of python programs in php mainly relies on program execution functions.
Here are three related functions: exec(), system() and passthru().
Here we mainly talk about the exec() function, introducing the use of this function to pass parameters,
and how to use python to return josn data for PHP to use.
Execute an external program
exec ( string $command [, array &$output [, int &$return_var ]] ) : string
Parameter description:
command: the command to be executed, It includes three substrings. The first substring is the interpreter of the current system used, the second substring is the location of the script to be executed, and the third substring is an unlimited number of parameters that need to be passed in, with spaces in between. Separate and pay attention to the format. Use spaces to separate substrings.
output: If the output parameter is provided, this array will be filled with the output of the command execution, one element in the array for each line of output. (Note: What is stored in output is not the value of return in python, and all return values will not be saved. What is stored in output is the value output in the python script, which is all the data output by the print() function)
return_var: If both output and return_var parameters are provided, the return status after command execution will be written to this variable.
index.php
<?php $re = exec('python ceshi.py', $out); // $re = iconv('gbk', 'utf-8', $re); var_dump($out); echo '<br/>'; var_dump($re);
ceshi.py
def send(): data = '1,2,3,4,5' print(data) if __name__ == '__main__': send()
(Important note: If the data returned by the Python script contains Chinese, Need to use iconv('gbk', 'utf-8', $re); for escaping)
inde.php
$canshu1 = '这是PHP传过来的参数'; $canshu2 = date('Y-m-d'); $re = exec("python ceshi.py $canshu1 $canshu2"); $re = iconv('gbk', 'utf-8', $re); var_dump($re);
test.py
import sys def send(): # a1 = sys.argv[1] # a2 = sys.argv[2] re = sys.argv[1:] data = '1,2,3,4,5,' + ','.join(re) # 转字符串 print(data) if __name__ == '__main__': send()
Import the sys package and use the sys.argv[] array to get the incoming parameters. The first incoming parameter is sys.argv[1] , the second one is sys.argv[2] and so on. Do not use argv[0]
to receive the returned json data:
import sys import json def send(): dict = {'id':111, 'title':'测试title'} dict['data'] = sys.argv[1:] jsonArr = json.dumps(dict, ensure_ascii=False) print(jsonArr) if __name__ == '__main__': send()
(involving When using Chinese characters, you need to specify ensure_ascii=False)
Execute the external program and display the output
system ( string $command [, int &$return_var ] ) : string
Same as the C version of the system() function, this function executes the command specified by the command parameter and outputs the execution result.
If PHP is running in the server module, the system() function will also try to automatically refresh the web server's output cache after each line of output.
If you want to get the raw output of a command without any processing, please use the passthru() function.
index.php
<?php echo '这是运行直接输出:'; $re = system('python ceshi.py'); // $re = iconv('gbk', 'utf-8', $re); echo '<br/>'; echo '这是赋值输出:'; var_dump($re);
The original version of test.py is used here, and the output effect is as follows:
Execute external programs and display output
passthru ( string $command [, int &$return_var ] ) : void
Similar to the exec() function, the passthru() function is also used to execute external commands (commands). When the executed Unix command outputs binary data and needs to be transmitted directly to the browser, this function needs to be used instead of the exec() or system() function. Commonly used to execute commands such as pbmplus that can directly output image streams. By setting Content-type to image/gif, and then calling the pbmplus program to output a gif file, you can directly output the image to the browser from the PHP script.
index.php
echo '这是运行直接输出:'; $re = passthru('python ceshi.py'); // $re = iconv('gbk', 'utf-8', $re); echo '<br/>'; echo '这是赋值输出:'; var_dump($re);
The original version of test.py is used here, and the output effect is as follows:
(Recommended tutorial: PHP video tutorial)
The above is the detailed content of How to run Python script in PHP program. For more information, please follow other related articles on the PHP Chinese website!