Home  >  Article  >  Backend Development  >  How to run Python script in PHP program

How to run Python script in PHP program

WBOY
WBOYforward
2022-09-09 13:48:592580browse

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

1. exec()

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.

1. Directly run

index.php

<?php
$re = exec(&#39;python ceshi.py&#39;, $out);
// $re = iconv(&#39;gbk&#39;, &#39;utf-8&#39;, $re);
var_dump($out);
echo &#39;<br/>&#39;;
var_dump($re);

ceshi.py

def send():
    data = &#39;1,2,3,4,5&#39;
    print(data)
if __name__ == &#39;__main__&#39;:
    send()

(Important note: If the data returned by the Python script contains Chinese, Need to use iconv('gbk', 'utf-8', $re); for escaping)

2. Pass parameters and receive return data

inde.php

$canshu1 = &#39;这是PHP传过来的参数&#39;;
$canshu2 = date(&#39;Y-m-d&#39;);
$re = exec("python ceshi.py $canshu1 $canshu2");
$re = iconv(&#39;gbk&#39;, &#39;utf-8&#39;, $re);
var_dump($re);

test.py

import sys
def send():
    # a1 = sys.argv[1]
    # a2 = sys.argv[2]
    re = sys.argv[1:]
    data = &#39;1,2,3,4,5,&#39; + &#39;,&#39;.join(re) # 转字符串
    print(data)
if __name__ == &#39;__main__&#39;:
    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 = {&#39;id&#39;:111, &#39;title&#39;:&#39;测试title&#39;}
    dict[&#39;data&#39;] = sys.argv[1:]
    jsonArr = json.dumps(dict, ensure_ascii=False)
    print(jsonArr)
if __name__ == &#39;__main__&#39;:
    send()

(involving When using Chinese characters, you need to specify ensure_ascii=False)

2. system()

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 &#39;这是运行直接输出:&#39;;
$re = system(&#39;python ceshi.py&#39;);
// $re = iconv(&#39;gbk&#39;, &#39;utf-8&#39;, $re);
echo &#39;<br/>&#39;;
echo &#39;这是赋值输出:&#39;;
var_dump($re);

The original version of test.py is used here, and the output effect is as follows:

3. passthru()

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 &#39;这是运行直接输出:&#39;;
$re = passthru(&#39;python ceshi.py&#39;);
// $re = iconv(&#39;gbk&#39;, &#39;utf-8&#39;, $re);
echo &#39;<br/>&#39;;
echo &#39;这是赋值输出:&#39;;
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!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete