forked from themeum/tutor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpHelper.php
More file actions
198 lines (175 loc) · 3.72 KB
/
Copy pathHttpHelper.php
File metadata and controls
198 lines (175 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
/**
* Helper class to manager HTTP request.
*
* @package Tutor\Helper
* @author Themeum <[email protected]>
* @link https://themeum.com
* @since 3.0.0
*/
namespace Tutor\Helpers;
/**
* HttpHelper class
*
* @since 3.0.0
*/
class HttpHelper {
/**
* HTTP methods constants
*
* @var string
*/
const METHOD_GET = 'GET';
const METHOD_POST = 'POST';
const METHOD_PUT = 'PUT';
const METHOD_PATCH = 'PATCH';
const METHOD_DELETE = 'DELETE';
/**
* 200 serial HTTP status code constants
*/
const STATUS_OK = 200;
const STATUS_CREATED = 201;
const STATUS_ACCEPTED = 202;
/**
* 400 serial HTTP status code constants
*/
const STATUS_BAD_REQUEST = 400;
const STATUS_UNAUTHORIZED = 401;
const STATUS_FORBIDDEN = 403;
const STATUS_NOT_FOUND = 404;
const STATUS_METHOD_NOT_ALLOWED = 405;
const STATUS_TOO_MANY_REQUESTS = 429;
const STATUS_UNPROCESSABLE_ENTITY = 422;
/**
* 500 serial HTTP status code constants
*/
const STATUS_INTERNAL_SERVER_ERROR = 500;
const STATUS_SERVICE_UNAVAILABLE = 503;
const STATUS_BAD_GATEWAY = 502;
const STATUS_GATEWAY_TIMEOUT = 504;
/**
* Response body
*
* @var mixed
*/
private $body;
/**
* Response headers
*
* @var mixed
*/
private $headers;
/**
* Response status code
*
* @var int
*/
private $status_code;
/**
* Hold WP error for request.
*
* @var \WP_Error
*/
private $wp_error;
/**
* Parse response from HTTP response.
*
* @param mixed $response response of request.
*
* @return void
*/
private function parse_response( $response ) {
if ( is_wp_error( $response ) ) {
$this->wp_error = $response;
} else {
$this->body = wp_remote_retrieve_body( $response );
$this->headers = wp_remote_retrieve_headers( $response );
$this->status_code = wp_remote_retrieve_response_code( $response );
}
}
/**
* Make HTTP GET request.
*
* @param string $url The URL for the request.
* @param array $data Optional. The data to include in the request (added to the URL as query parameters).
* @param array $headers Optional. Additional headers for the request.
*
* @return self
*/
public static function get( $url, $data = array(), $headers = array() ) {
$url_with_params = add_query_arg( $data, $url );
$response = wp_remote_get( $url_with_params, array( 'headers' => $headers ) );
$self = new self();
$self->parse_response( $response );
return $self;
}
/**
* Make HTTP POST request.
*
* @param string $url The URL for the request.
* @param array $data Optional. The data to include in the request body.
* @param array $headers Optional. Additional headers for the request.
*
* @return self
*/
public static function post( $url, $data = array(), $headers = array() ) {
$response = wp_remote_post(
$url,
array(
'body' => $data,
'headers' => $headers,
)
);
$self = new self();
$self->parse_response( $response );
return $self;
}
/**
* Get body
*
* @return mixed
*/
public function get_body() {
return $this->body;
}
/**
* Get body data as JSON
*
* @return mixed
*/
public function get_json() {
return json_decode( $this->body );
}
/**
* Get headers
*
* @return mixed
*/
public function get_headers() {
return $this->headers;
}
/**
* Get status code.
*
* @return int
*/
public function get_status_code() {
return $this->status_code;
}
/**
* Check any error occur.
*
* @return boolean
*/
public function has_error() {
return ! is_null( $this->wp_error );
}
/**
* Get error message.
*
* @return mixed
*/
public function get_error_message() {
return $this->wp_error->get_error_message();
}
}