· 6 years ago · Sep 21, 2019, 01:16 PM
1<?php
2
3
4class Api
5{
6
7
8 /**
9 * API base url
10 *
11 * @access private
12 * @static
13 * @var string $api
14 */
15 private static $api = "http://127.0.0.1:8080";
16
17
18 /**
19 * CURL function used to get RAW data
20 *
21 * @access protected
22 * @static
23 * @param string $method - request method used , default is GET
24 * @param string $url - api url ( part ) used to proceed and get data
25 * @return array
26 */
27 protected static function curl(string $method = 'GET', string $url):array
28 {
29 $curl = curl_init();
30
31 curl_setopt_array($curl, array(
32 CURLOPT_URL => $api . $url,
33 CURLOPT_RETURNTRANSFER => true,
34 CURLOPT_TIMEOUT => 30,
35 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
36 CURLOPT_CUSTOMREQUEST => strtoupper($method),
37 CURLOPT_HTTPHEADER => array(
38 "cache-control: no-cache"
39 ),
40 ));
41
42 $response = curl_exec($curl);
43 $error = curl_error($curl);
44
45 curl_close($curl);
46
47 return [
48 'error' => $error,
49 'response' => $response
50 ];
51 }
52
53
54 /**
55 * Method to check if defined api key is the correct
56 *
57 * @access public
58 * @static
59 * @param string $apiKey - api key for the current domain
60 * @return bool
61 */
62 public static function checkApiKey(string $apiKey):bool
63 {
64 $url = self::$api . '/auth/' . $apiKey;
65 return self::curl( 'GET', $url );
66 }
67
68
69 /**
70 * Data getter
71 *
72 * @access public
73 * @static
74 * @param string $short - the short uniq code
75 * @param bool $array - define if return data is array or object
76 * @return object
77 */
78 public static function getData(string $short, bool $array = FALSE):object
79 {
80 return json_decode( self::curl($short) , $array );
81 }
82
83
84}