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