avm99963 | 99bb77c | 2020-01-27 03:15:08 +0100 | [diff] [blame^] | 1 | <?php |
| 2 | class tmbApi { |
| 3 | const API_ENDPOINT = "https://api.tmb.cat/v1/"; |
| 4 | |
| 5 | public static function httpRequest($url, $method = "GET", $params = [], $file = null) { |
| 6 | $curl = curl_init(); |
| 7 | curl_setopt_array($curl, [ |
| 8 | CURLOPT_RETURNTRANSFER => 1, |
| 9 | CURLOPT_URL => $url |
| 10 | ]); |
| 11 | |
| 12 | if ($method === "POST") { |
| 13 | curl_setopt($curl, CURLOPT_POST, 1); |
| 14 | if (!empty($params)) curl_setopt($curl, CURLOPT_POST_PARAMS, $params); |
| 15 | } |
| 16 | |
| 17 | |
| 18 | if ($file !== null) { |
| 19 | curl_setopt($curl, CURLOPT_FILE, $file); |
| 20 | } |
| 21 | |
| 22 | $response = curl_exec($curl); |
| 23 | curl_close($curl); |
| 24 | |
| 25 | if ($file === null) return $response; |
| 26 | return true; |
| 27 | } |
| 28 | |
| 29 | public static function httpJSONRequest($url, $method = "GET", $params = []) { |
| 30 | $json = json_decode(self::httpRequest($url, $method, $params), true); |
| 31 | if (json_last_error() !== JSON_ERROR_NONE) return false; |
| 32 | |
| 33 | return $json; |
| 34 | } |
| 35 | |
| 36 | public static function request($action, $file = null) { |
| 37 | global $conf; |
| 38 | |
| 39 | $url = self::API_ENDPOINT.$action."?app_id=".urlencode($conf["tmbApi"]["appId"])."&app_key=".urlencode($conf["tmbApi"]["appKey"]); |
| 40 | |
| 41 | if ($file === null) return self::httpJSONRequest($url); |
| 42 | self::httpRequest($url, "GET", [], $file); |
| 43 | } |
| 44 | } |