-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
50 lines (39 loc) · 1.5 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
// 接收并解码JSON数据
$json = file_get_contents('php://input');
// 日志文件路径
$logFile = 'server.log';
// 打开进程来运行Python脚本,并通过管道传递JSON数据
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("pipe", "w") // stderr is a pipe that the child will write to
);
$process = proc_open('python3 gitlab2feishu.py', $descriptorspec, $pipes);
if (is_resource($process)) {
// 向Python脚本传递JSON数据
fwrite($pipes[0], $json);
fclose($pipes[0]);
// 读取Python脚本的输出
$stdout = stream_get_contents($pipes[1]);
fclose($pipes[1]);
// 读取Python脚本的错误输出(如果有)
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[2]);
// 等待进程执行完成
$return_value = proc_close($process);
// 将输出和错误(如果有)写入日志文件
if (!empty($stdout)) {
file_put_contents($logFile, "STDOUT: " . $stdout . PHP_EOL, FILE_APPEND);
}
if (!empty($stderr)) {
file_put_contents($logFile, "STDERR: " . $stderr . PHP_EOL, FILE_APPEND);
}
// 返回HTTP 200状态码
http_response_code(200);
} else {
// 进程创建失败,记录错误并返回适当的HTTP状态码
file_put_contents($logFile, "Failed to open process." . PHP_EOL, FILE_APPEND);
http_response_code(500); // Internal Server Error
}
?>