-
Notifications
You must be signed in to change notification settings - Fork 3
/
jq.php
50 lines (38 loc) · 1.12 KB
/
jq.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
<?php
function call_jq($contents, $filter)
{
$filter = escapeshellarg($filter);
$cmd = './jq ' . $filter;
$temp = tmpfile();
$filename = stream_get_meta_data($temp)['uri'];
fwrite($temp, $contents);
$filename = escapeshellarg($filename);
$cmd .= " --compact-output $filename";
exec($cmd, $output, $return);
/**
* Normally jq exits with 2 if there was any usage problem or system error, 3 if there was a jq program compile error, or 0
* if the jq program ran.
*/
switch ($return) {
case 2:
case 3:
http_response_code(400);
$error = join("\n", $output);
return 'jq eror: '. $error;
break;
case 0:
default:
if(count($output) === 1 and $return === 0)
{
// Just guessing
header("Content-Type: application/json");
return $output[0];
}
else
{
header("Content-Type: application/x-ndjson");
return join("\n", $output);
}
break;
}
}