DateTimeHelper.php
1 year ago
HttpHelper.php
1 year ago
QueryHelper.php
1 year ago
SessionHelper.php
3 years 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 |