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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 1 year ago HttpHelper.php 1 year ago PluginInstaller.php 1 year ago QueryHelper.php 1 year ago SessionHelper.php 3 years ago TemplateImportHelper.php 1 year ago ValidationHelper.php 1 year ago
HttpHelper.php
188 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 * 200 serial HTTP status code constants
21 */
22 const STATUS_OK = 200;
23 const STATUS_CREATED = 201;
24 const STATUS_ACCEPTED = 202;
25
26 /**
27 * 400 serial HTTP status code constants
28 */
29 const STATUS_BAD_REQUEST = 400;
30 const STATUS_UNAUTHORIZED = 401;
31 const STATUS_FORBIDDEN = 403;
32 const STATUS_NOT_FOUND = 404;
33 const STATUS_METHOD_NOT_ALLOWED = 405;
34 const STATUS_TOO_MANY_REQUESTS = 429;
35 const STATUS_UNPROCESSABLE_ENTITY = 422;
36
37 /**
38 * 500 serial HTTP status code constants
39 */
40 const STATUS_INTERNAL_SERVER_ERROR = 500;
41 const STATUS_SERVICE_UNAVAILABLE = 503;
42 const STATUS_BAD_GATEWAY = 502;
43 const STATUS_GATEWAY_TIMEOUT = 504;
44
45 /**
46 * Response body
47 *
48 * @var mixed
49 */
50 private $body;
51
52 /**
53 * Response headers
54 *
55 * @var mixed
56 */
57 private $headers;
58
59 /**
60 * Response status code
61 *
62 * @var int
63 */
64 private $status_code;
65
66 /**
67 * Hold WP error for request.
68 *
69 * @var \WP_Error
70 */
71 private $wp_error;
72
73 /**
74 * Parse response from HTTP response.
75 *
76 * @param mixed $response response of request.
77 *
78 * @return void
79 */
80 private function parse_response( $response ) {
81 if ( is_wp_error( $response ) ) {
82 $this->wp_error = $response;
83 } else {
84 $this->body = wp_remote_retrieve_body( $response );
85 $this->headers = wp_remote_retrieve_headers( $response );
86 $this->status_code = wp_remote_retrieve_response_code( $response );
87 }
88 }
89
90 /**
91 * Make HTTP GET request.
92 *
93 * @param string $url The URL for the request.
94 * @param array $data Optional. The data to include in the request (added to the URL as query parameters).
95 * @param array $headers Optional. Additional headers for the request.
96 *
97 * @return self
98 */
99 public static function get( $url, $data = array(), $headers = array() ) {
100 $url_with_params = add_query_arg( $data, $url );
101
102 $response = wp_remote_get( $url_with_params, array( 'headers' => $headers ) );
103
104 $self = new self();
105 $self->parse_response( $response );
106
107 return $self;
108 }
109
110 /**
111 * Make HTTP POST request.
112 *
113 * @param string $url The URL for the request.
114 * @param array $data Optional. The data to include in the request body.
115 * @param array $headers Optional. Additional headers for the request.
116 *
117 * @return self
118 */
119 public static function post( $url, $data = array(), $headers = array() ) {
120 $response = wp_remote_post(
121 $url,
122 array(
123 'body' => $data,
124 'headers' => $headers,
125 )
126 );
127
128 $self = new self();
129 $self->parse_response( $response );
130
131 return $self;
132 }
133
134 /**
135 * Get body
136 *
137 * @return mixed
138 */
139 public function get_body() {
140 return $this->body;
141 }
142
143 /**
144 * Get body data as JSON
145 *
146 * @return mixed
147 */
148 public function get_json() {
149 return json_decode( $this->body );
150 }
151
152 /**
153 * Get headers
154 *
155 * @return mixed
156 */
157 public function get_headers() {
158 return $this->headers;
159 }
160
161 /**
162 * Get status code.
163 *
164 * @return int
165 */
166 public function get_status_code() {
167 return $this->status_code;
168 }
169
170 /**
171 * Check any error occur.
172 *
173 * @return boolean
174 */
175 public function has_error() {
176 return ! is_null( $this->wp_error );
177 }
178
179 /**
180 * Get error message.
181 *
182 * @return mixed
183 */
184 public function get_error_message() {
185 return $this->wp_error->get_error_message();
186 }
187 }
188