Copybara bot | be50d49 | 2023-11-30 00:16:42 +0100 | [diff] [blame^] | 1 | <?php |
| 2 | class api { |
| 3 | public static function inputJson() { |
| 4 | $string = trim(file_get_contents("php://input")); |
| 5 | |
| 6 | if (empty($string)) return false; |
| 7 | |
| 8 | $json = json_decode($string, true); |
| 9 | |
| 10 | if (json_last_error() !== JSON_ERROR_NONE) return false; |
| 11 | |
| 12 | return $json; |
| 13 | } |
| 14 | |
| 15 | public static function error($message = null) { |
| 16 | if ($message !== null) self::write([ |
| 17 | 'error' => true, |
| 18 | 'message' => $message, |
| 19 | ]); |
| 20 | http_response_code(400); |
| 21 | exit(); |
| 22 | } |
| 23 | |
| 24 | public static function write($array) { |
| 25 | header('Content-Type: application/json'); |
| 26 | echo json_encode($array); |
| 27 | } |
| 28 | } |