-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstream_input_v1.php
More file actions
49 lines (42 loc) · 1.03 KB
/
stream_input_v1.php
File metadata and controls
49 lines (42 loc) · 1.03 KB
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
<?php
// Path to your FIFO
$pipe = "/tmp/pipe2.pipe";
//No API or validation
//Bash commands to setup pipe
//rm -f /tmp/pipe2.pipe && mkfifo /tmp/pipe2.pipe && echo "test" > /tmp/pipe2.pipe
//chown www-data:www-data /tmp/pipe2.pipe
//chmod 666 /tmp/pipe2.pipe
//
// Get the raw POST data
$rawData = file_get_contents("php://input");
// Decode JSON
$data = json_decode($rawData, true);
if ($data === null) {
http_response_code(400);
echo "Invalid JSON";
exit;
}
$mode=0600;
if(!file_exists($pipe)) {
// create the pipe
umask(0);
posix_mkfifo($pipe,$mode);
}
// Re-encode to JSON string (optional, or just send text/event fields separately)
$line = json_encode($data) . "\n";
// Write to FIFO
if (file_exists($pipe)) {
$fp = fopen($pipe, 'w');
if ($fp) {
fwrite($fp, $line);
fclose($fp);
echo "Written to FIFO";
} else {
http_response_code(500);
echo "Unable to write to FIFO";
}
} else {
http_response_code(500);
echo "FIFO not found";
}
?>