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 |