1 | <?php |
||
13 | class HTTP { |
||
14 | use Module; |
||
15 | |||
16 | protected static $UA = "Mozilla/4.0 (compatible; Core::HTTP; Windows NT 6.1)", |
||
17 | $json_data = false, |
||
18 | $headers = [], |
||
19 | $last_info = null; |
||
20 | |||
21 | protected static function request($method, $url, $data=[], array $headers=[], $data_as_json=false, $username=null, $password = null){ |
||
22 | $http_method = strtoupper($method); |
||
23 | $ch = curl_init($url); |
||
24 | $opt = [ |
||
25 | CURLOPT_CUSTOMREQUEST => $http_method, |
||
26 | CURLOPT_SSL_VERIFYHOST => false, |
||
27 | CURLOPT_CONNECTTIMEOUT => 10, |
||
28 | CURLOPT_RETURNTRANSFER => true, |
||
29 | CURLOPT_USERAGENT => static::$UA, |
||
30 | CURLOPT_HEADER => false, |
||
31 | CURLOPT_MAXREDIRS => 10, |
||
32 | CURLOPT_FOLLOWLOCATION => true, |
||
33 | CURLOPT_ENCODING => '', |
||
34 | ]; |
||
35 | |||
36 | if($username && $password) { |
||
37 | $opt[CURLOPT_USERPWD] = "$username:$password"; |
||
38 | } |
||
39 | |||
40 | $headers = array_merge($headers,static::$headers); |
||
41 | |||
42 | if($http_method == 'GET'){ |
||
43 | if($data && is_array($data)){ |
||
44 | $tmp = []; |
||
45 | $queried_url = $url; |
||
46 | foreach($data as $key=>$val) $tmp[] = $key.'='.$val; |
||
47 | $queried_url .= (strpos($queried_url,'?') === false) ? '?' : '&'; |
||
48 | $queried_url .= implode('&',$tmp); |
||
49 | $opt[CURLOPT_URL] = $queried_url; |
||
50 | $opt[CURLOPT_HTTPGET] = true; |
||
51 | unset($opt[CURLOPT_CUSTOMREQUEST]); |
||
52 | } |
||
53 | } else { |
||
54 | $opt[CURLOPT_CUSTOMREQUEST] = $http_method; |
||
55 | if($data_as_json or is_object($data)){ |
||
56 | $headers['Content-Type'] = 'application/json'; |
||
57 | $opt[CURLOPT_POSTFIELDS] = json_encode($data); |
||
58 | } else { |
||
59 | $opt[CURLOPT_POSTFIELDS] = http_build_query($data); |
||
60 | } |
||
61 | } |
||
62 | |||
63 | curl_setopt_array($ch,$opt); |
||
64 | $_harr = []; |
||
65 | foreach($headers as $key=>$val) $_harr[] = $key.': '.$val; |
||
66 | curl_setopt($ch, CURLOPT_HTTPHEADER, $_harr); |
||
67 | $result = curl_exec($ch); |
||
68 | $contentType = strtolower(curl_getinfo($ch, CURLINFO_CONTENT_TYPE)); |
||
69 | static::$last_info = curl_getinfo($ch); |
||
70 | if(false !== strpos($contentType,'json')) $result = json_decode($result); |
||
71 | curl_close($ch); |
||
72 | return $result; |
||
73 | } |
||
74 | |||
75 | public static function useJSON($value=null){ |
||
76 | return $value===null ? static::$json_data : static::$json_data = $value; |
||
77 | } |
||
78 | |||
79 | public static function addHeader($name,$value){ |
||
80 | static::$headers[$name] = $value; |
||
81 | } |
||
82 | |||
83 | public static function removeHeader($name){ |
||
84 | unset(static::$headers[$name]); |
||
85 | } |
||
86 | |||
87 | public static function headers($name=null){ |
||
88 | // null === $name ?? static::$headers ?? static::$headers[$name] |
||
89 | return null === $name |
||
90 | ? static::$headers |
||
91 | : ( isset(static::$headers[$name]) ? static::$headers[$name] : '' ); |
||
92 | } |
||
93 | |||
94 | public static function userAgent($value=null){ |
||
95 | return $value===null ? static::$UA : static::$UA = $value; |
||
96 | } |
||
97 | |||
98 | public static function get($url, $data=null, array $headers=[], $username = null, $password = null){ |
||
99 | return static::request('get',$url,$data,$headers,false,$username,$password); |
||
100 | } |
||
101 | |||
102 | public static function post($url, $data=null, array $headers=[], $username = null, $password = null){ |
||
103 | return static::request('post',$url,$data,$headers,static::$json_data,$username,$password); |
||
104 | } |
||
105 | |||
106 | public static function put($url, $data=null, array $headers=[], $username = null, $password = null){ |
||
107 | return static::request('put',$url,$data,$headers,static::$json_data,$username,$password); |
||
108 | } |
||
109 | |||
110 | public static function delete($url, $data=null, array $headers=[], $username = null, $password = null){ |
||
111 | return static::request('delete',$url,$data,$headers,static::$json_data,$username,$password); |
||
112 | } |
||
113 | |||
114 | public static function info($url = null){ |
||
115 | if ($url){ |
||
116 | curl_setopt_array($ch = curl_init($url), [ |
||
117 | CURLOPT_SSL_VERIFYHOST => false, |
||
118 | CURLOPT_CONNECTTIMEOUT => 10, |
||
119 | CURLOPT_RETURNTRANSFER => true, |
||
120 | CURLOPT_USERAGENT => static::$UA, |
||
121 | CURLOPT_HEADER => false, |
||
122 | CURLOPT_ENCODING => '', |
||
123 | CURLOPT_FILETIME => true, |
||
124 | CURLOPT_NOBODY => true, |
||
125 | ]); |
||
126 | curl_exec($ch); |
||
127 | $info = curl_getinfo($ch); |
||
128 | curl_close($ch); |
||
129 | return $info; |
||
130 | } else { |
||
131 | return static::$last_info; |
||
132 | } |
||
133 | } |
||
134 | |||
135 | } |
||
136 | |||
156 |
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString
.