Completed
Push — master ( 1e9fe6...efe7e0 )
by Stefano
03:52
created
classes/HTTP.php 3 patches
Indentation   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -14,9 +14,9 @@
 block discarded – undo
14 14
   use Module;
15 15
 
16 16
   protected static $UA          = "Mozilla/4.0 (compatible; Core::HTTP; Windows NT 6.1)",
17
-                   $json_data   = false,
18
-                   $headers     = [],
19
-                   $last_info   = null;
17
+                    $json_data   = false,
18
+                    $headers     = [],
19
+                    $last_info   = null;
20 20
 
21 21
   protected static function request($method, $url, $data=[], array $headers=[], $data_as_json=false, $username=null, $password = null){
22 22
     $http_method = strtoupper($method);
Please login to merge, or discard this patch.
Spacing   +30 added lines, -30 removed lines patch added patch discarded remove patch
@@ -18,7 +18,7 @@  discard block
 block discarded – undo
18 18
                    $headers     = [],
19 19
                    $last_info   = null;
20 20
 
21
-  protected static function request($method, $url, $data=[], array $headers=[], $data_as_json=false, $username=null, $password = null){
21
+  protected static function request($method, $url, $data = [], array $headers = [], $data_as_json = false, $username = null, $password = null) {
22 22
     $http_method = strtoupper($method);
23 23
     $ch  = curl_init($url);
24 24
     $opt = [
@@ -33,26 +33,26 @@  discard block
 block discarded – undo
33 33
       CURLOPT_ENCODING        => '',
34 34
     ];
35 35
 
36
-    if($username && $password) {
36
+    if ($username && $password) {
37 37
       $opt[CURLOPT_USERPWD] = "$username:$password";
38 38
     }
39 39
 
40
-    $headers = array_merge($headers,static::$headers);
40
+    $headers = array_merge($headers, static::$headers);
41 41
 
42
-    if($http_method == 'GET'){
43
-        if($data && is_array($data)){
42
+    if ($http_method == 'GET') {
43
+        if ($data && is_array($data)) {
44 44
           $tmp                       = [];
45 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);
46
+          foreach ($data as $key=>$val) $tmp[] = $key.'='.$val;
47
+          $queried_url               .= (strpos($queried_url, '?') === false) ? '?' : '&';
48
+          $queried_url               .= implode('&', $tmp);
49 49
           $opt[CURLOPT_URL]          = $queried_url;
50 50
           $opt[CURLOPT_HTTPGET]      = true;
51 51
           unset($opt[CURLOPT_CUSTOMREQUEST]);
52 52
         }
53 53
     } else {
54 54
         $opt[CURLOPT_CUSTOMREQUEST]  = $http_method;
55
-        if($data_as_json or is_object($data)){
55
+        if ($data_as_json or is_object($data)) {
56 56
           $headers['Content-Type']   = 'application/json';
57 57
           $opt[CURLOPT_POSTFIELDS]   = json_encode($data);
58 58
         } else {
@@ -60,59 +60,59 @@  discard block
 block discarded – undo
60 60
         }
61 61
     }
62 62
 
63
-    curl_setopt_array($ch,$opt);
63
+    curl_setopt_array($ch, $opt);
64 64
     $_harr = [];
65
-    foreach($headers as $key=>$val)  $_harr[] = $key.': '.$val;
65
+    foreach ($headers as $key=>$val)  $_harr[] = $key.': '.$val;
66 66
     curl_setopt($ch, CURLOPT_HTTPHEADER, $_harr);
67 67
     $result = curl_exec($ch);
68 68
     $contentType = strtolower(curl_getinfo($ch, CURLINFO_CONTENT_TYPE));
69 69
     static::$last_info = curl_getinfo($ch);
70
-    if(false !== strpos($contentType,'json')) $result = json_decode($result);
70
+    if (false !== strpos($contentType, 'json')) $result = json_decode($result);
71 71
     curl_close($ch);
72 72
     return $result;
73 73
   }
74 74
 
75
-  public static function useJSON($value=null){
76
-    return $value===null ? static::$json_data : static::$json_data = $value;
75
+  public static function useJSON($value = null) {
76
+    return $value === null ? static::$json_data : static::$json_data = $value;
77 77
   }
78 78
 
79
-  public static function addHeader($name,$value){
79
+  public static function addHeader($name, $value) {
80 80
     static::$headers[$name] = $value;
81 81
   }
82 82
 
83
-  public static function removeHeader($name){
83
+  public static function removeHeader($name) {
84 84
     unset(static::$headers[$name]);
85 85
   }
86 86
 
87
-  public static function headers($name=null){
87
+  public static function headers($name = null) {
88 88
     // null === $name ?? static::$headers ?? static::$headers[$name]
89 89
     return null === $name
90 90
            ? static::$headers
91
-           : ( isset(static::$headers[$name]) ? static::$headers[$name] : '' );
91
+           : (isset(static::$headers[$name]) ? static::$headers[$name] : '');
92 92
   }
93 93
 
94
-  public static function userAgent($value=null){
95
-    return $value===null ? static::$UA : static::$UA = $value;
94
+  public static function userAgent($value = null) {
95
+    return $value === null ? static::$UA : static::$UA = $value;
96 96
   }
97 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);
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 100
   }
101 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);
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 104
   }
105 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);
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 108
   }
109 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);
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 112
   }
113 113
 
114
-  public static function info($url = null){
115
-    if ($url){
114
+  public static function info($url = null) {
115
+    if ($url) {
116 116
       curl_setopt_array($ch = curl_init($url), [
117 117
         CURLOPT_SSL_VERIFYHOST  => false,
118 118
         CURLOPT_CONNECTTIMEOUT  => 10,
Please login to merge, or discard this patch.
Braces   +18 added lines, -16 removed lines patch added patch discarded remove patch
@@ -18,7 +18,7 @@  discard block
 block discarded – undo
18 18
                    $headers     = [],
19 19
                    $last_info   = null;
20 20
 
21
-  protected static function request($method, $url, $data=[], array $headers=[], $data_as_json=false, $username=null, $password = null){
21
+  protected static function request($method, $url, $data=[], array $headers=[], $data_as_json=false, $username=null, $password = null) {
22 22
     $http_method = strtoupper($method);
23 23
     $ch  = curl_init($url);
24 24
     $opt = [
@@ -39,8 +39,8 @@  discard block
 block discarded – undo
39 39
 
40 40
     $headers = array_merge($headers,static::$headers);
41 41
 
42
-    if($http_method == 'GET'){
43
-        if($data && is_array($data)){
42
+    if($http_method == 'GET') {
43
+        if($data && is_array($data)) {
44 44
           $tmp                       = [];
45 45
           $queried_url               = $url;
46 46
           foreach($data as $key=>$val) $tmp[] = $key.'='.$val;
@@ -52,7 +52,7 @@  discard block
 block discarded – undo
52 52
         }
53 53
     } else {
54 54
         $opt[CURLOPT_CUSTOMREQUEST]  = $http_method;
55
-        if($data_as_json or is_object($data)){
55
+        if($data_as_json or is_object($data)) {
56 56
           $headers['Content-Type']   = 'application/json';
57 57
           $opt[CURLOPT_POSTFIELDS]   = json_encode($data);
58 58
         } else {
@@ -67,52 +67,54 @@  discard block
 block discarded – undo
67 67
     $result = curl_exec($ch);
68 68
     $contentType = strtolower(curl_getinfo($ch, CURLINFO_CONTENT_TYPE));
69 69
     static::$last_info = curl_getinfo($ch);
70
-    if(false !== strpos($contentType,'json')) $result = json_decode($result);
70
+    if(false !== strpos($contentType,'json')) {
71
+      $result = json_decode($result);
72
+    }
71 73
     curl_close($ch);
72 74
     return $result;
73 75
   }
74 76
 
75
-  public static function useJSON($value=null){
77
+  public static function useJSON($value=null) {
76 78
     return $value===null ? static::$json_data : static::$json_data = $value;
77 79
   }
78 80
 
79
-  public static function addHeader($name,$value){
81
+  public static function addHeader($name,$value) {
80 82
     static::$headers[$name] = $value;
81 83
   }
82 84
 
83
-  public static function removeHeader($name){
85
+  public static function removeHeader($name) {
84 86
     unset(static::$headers[$name]);
85 87
   }
86 88
 
87
-  public static function headers($name=null){
89
+  public static function headers($name=null) {
88 90
     // null === $name ?? static::$headers ?? static::$headers[$name]
89 91
     return null === $name
90 92
            ? static::$headers
91 93
            : ( isset(static::$headers[$name]) ? static::$headers[$name] : '' );
92 94
   }
93 95
 
94
-  public static function userAgent($value=null){
96
+  public static function userAgent($value=null) {
95 97
     return $value===null ? static::$UA : static::$UA = $value;
96 98
   }
97 99
 
98
-  public static function get($url, $data=null, array $headers=[], $username = null, $password = null){
100
+  public static function get($url, $data=null, array $headers=[], $username = null, $password = null) {
99 101
     return static::request('get',$url,$data,$headers,false,$username,$password);
100 102
   }
101 103
 
102
-  public static function post($url, $data=null, array $headers=[], $username = null, $password = null){
104
+  public static function post($url, $data=null, array $headers=[], $username = null, $password = null) {
103 105
     return static::request('post',$url,$data,$headers,static::$json_data,$username,$password);
104 106
   }
105 107
 
106
-  public static function put($url, $data=null, array $headers=[], $username = null, $password = null){
108
+  public static function put($url, $data=null, array $headers=[], $username = null, $password = null) {
107 109
     return static::request('put',$url,$data,$headers,static::$json_data,$username,$password);
108 110
   }
109 111
 
110
-  public static function delete($url, $data=null, array $headers=[], $username = null, $password = null){
112
+  public static function delete($url, $data=null, array $headers=[], $username = null, $password = null) {
111 113
     return static::request('delete',$url,$data,$headers,static::$json_data,$username,$password);
112 114
   }
113 115
 
114
-  public static function info($url = null){
115
-    if ($url){
116
+  public static function info($url = null) {
117
+    if ($url) {
116 118
       curl_setopt_array($ch = curl_init($url), [
117 119
         CURLOPT_SSL_VERIFYHOST  => false,
118 120
         CURLOPT_CONNECTTIMEOUT  => 10,
Please login to merge, or discard this patch.