| @@ -1,70 +1,71 @@ | ||
| 1 | 1 | <?php |
| 2 | + | |
| 2 | 3 | namespace BitCode\BitForm\Core\Util; |
| 3 | 4 | |
| 4 | 5 | final class HttpHelper |
| 5 | 6 | { |
| 6 | - public static function post($url, $data, $headers = null, $options = null) | |
| 7 | - { | |
| 8 | - $defaultOptions =[ | |
| 9 | - 'headers' => $headers, | |
| 10 | - 'body' => $data, | |
| 11 | - "timeout" => 30 | |
| 12 | - ]; | |
| 7 | + public static function post($url, $data, $headers = null, $options = null) | |
| 8 | + { | |
| 9 | + $defaultOptions = [ | |
| 10 | + 'headers' => $headers, | |
| 11 | + 'body' => $data, | |
| 12 | + 'timeout' => 30 | |
| 13 | + ]; | |
| 13 | 14 | |
| 14 | - $options = wp_parse_args($options, $defaultOptions); | |
| 15 | - $requestReponse = wp_remote_post($url, $options); | |
| 16 | - if (is_wp_error($requestReponse)) { | |
| 17 | - return $requestReponse; | |
| 18 | - } | |
| 19 | - // $responseCode = wp_remote_retrieve_response_code($requestReponse); | |
| 20 | - // if (!\is_null($responseCode) && $responseCode != 200) { | |
| 21 | - // return wp_remote_retrieve_response_message($requestReponse); | |
| 22 | - // } | |
| 23 | - $responseBody = wp_remote_retrieve_body($requestReponse); | |
| 24 | - $jsonData = json_decode($responseBody); | |
| 25 | - return \is_null($jsonData) ? $responseBody : $jsonData; | |
| 15 | + $options = wp_parse_args($options, $defaultOptions); | |
| 16 | + $requestReponse = wp_remote_post($url, $options); | |
| 17 | + if (is_wp_error($requestReponse)) { | |
| 18 | + return $requestReponse; | |
| 26 | 19 | } |
| 20 | + // $responseCode = wp_remote_retrieve_response_code($requestReponse); | |
| 21 | + // if (!\is_null($responseCode) && $responseCode != 200) { | |
| 22 | + // return wp_remote_retrieve_response_message($requestReponse); | |
| 23 | + // } | |
| 24 | + $responseBody = wp_remote_retrieve_body($requestReponse); | |
| 25 | + $jsonData = json_decode($responseBody); | |
| 26 | + return \is_null($jsonData) ? $responseBody : $jsonData; | |
| 27 | + } | |
| 27 | 28 | |
| 28 | - public static function get($url, $data, $headers = null, $options = null) | |
| 29 | - { | |
| 30 | - $defaultOptions =[ | |
| 31 | - 'headers' => $headers, | |
| 32 | - 'body' => $data, | |
| 33 | - "timeout" => 30 | |
| 34 | - ]; | |
| 35 | - $options = wp_parse_args($options, $defaultOptions); | |
| 36 | - $requestReponse = wp_remote_get($url, $options); | |
| 37 | - if (is_wp_error($requestReponse)) { | |
| 38 | - return $requestReponse; | |
| 39 | - } | |
| 40 | - // $responseCode = wp_remote_retrieve_response_code($requestReponse); | |
| 41 | - // if (!\is_null($responseCode) && $responseCode != 200) { | |
| 42 | - // return wp_remote_retrieve_response_message($requestReponse); | |
| 43 | - // } | |
| 44 | - $responseBody = wp_remote_retrieve_body($requestReponse); | |
| 45 | - $jsonData = json_decode($responseBody); | |
| 46 | - return \is_null($jsonData) ? $responseBody : $jsonData; | |
| 29 | + public static function get($url, $data, $headers = null, $options = null) | |
| 30 | + { | |
| 31 | + $defaultOptions = [ | |
| 32 | + 'headers' => $headers, | |
| 33 | + 'body' => $data, | |
| 34 | + 'timeout' => 30 | |
| 35 | + ]; | |
| 36 | + $options = wp_parse_args($options, $defaultOptions); | |
| 37 | + $requestReponse = wp_remote_get($url, $options); | |
| 38 | + if (is_wp_error($requestReponse)) { | |
| 39 | + return $requestReponse; | |
| 47 | 40 | } |
| 41 | + // $responseCode = wp_remote_retrieve_response_code($requestReponse); | |
| 42 | + // if (!\is_null($responseCode) && $responseCode != 200) { | |
| 43 | + // return wp_remote_retrieve_response_message($requestReponse); | |
| 44 | + // } | |
| 45 | + $responseBody = wp_remote_retrieve_body($requestReponse); | |
| 46 | + $jsonData = json_decode($responseBody); | |
| 47 | + return \is_null($jsonData) ? $responseBody : $jsonData; | |
| 48 | + } | |
| 48 | 49 | |
| 49 | - public static function request($url, $type, $data, $headers = null, $options = null) | |
| 50 | - { | |
| 51 | - $defaultOptions =[ | |
| 52 | - 'method' => $type, | |
| 53 | - 'headers' => $headers, | |
| 54 | - 'body' => $data, | |
| 55 | - "timeout" => 30 | |
| 56 | - ]; | |
| 57 | - $options = wp_parse_args($options, $defaultOptions); | |
| 58 | - $requestReponse = wp_remote_request($url, $options); | |
| 59 | - if (is_wp_error($requestReponse)) { | |
| 60 | - return $requestReponse; | |
| 61 | - } | |
| 62 | - // $responseCode = wp_remote_retrieve_response_code($requestReponse); | |
| 63 | - // if (!\is_null($responseCode) && $responseCode != 200) { | |
| 64 | - // return wp_remote_retrieve_response_message($requestReponse); | |
| 65 | - // } | |
| 66 | - $responseBody = wp_remote_retrieve_body($requestReponse); | |
| 67 | - $jsonData = json_decode($responseBody); | |
| 68 | - return \is_null($jsonData) ? $responseBody : $jsonData; | |
| 50 | + public static function request($url, $type, $data, $headers = null, $options = null) | |
| 51 | + { | |
| 52 | + $defaultOptions = [ | |
| 53 | + 'method' => $type, | |
| 54 | + 'headers' => $headers, | |
| 55 | + 'body' => $data, | |
| 56 | + 'timeout' => 30 | |
| 57 | + ]; | |
| 58 | + $options = wp_parse_args($options, $defaultOptions); | |
| 59 | + $requestReponse = wp_remote_request($url, $options); | |
| 60 | + if (is_wp_error($requestReponse)) { | |
| 61 | + return $requestReponse; | |
| 69 | 62 | } |
| 63 | + // $responseCode = wp_remote_retrieve_response_code($requestReponse); | |
| 64 | + // if (!\is_null($responseCode) && $responseCode != 200) { | |
| 65 | + // return wp_remote_retrieve_response_message($requestReponse); | |
| 66 | + // } | |
| 67 | + $responseBody = wp_remote_retrieve_body($requestReponse); | |
| 68 | + $jsonData = json_decode($responseBody); | |
| 69 | + return \is_null($jsonData) ? $responseBody : $jsonData; | |
| 70 | + } | |
| 70 | 71 | } |