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