ApplyFiltersTrait.php
2 months ago
ArrayableTrait.php
7 months ago
BatchSizeCalculateTrait.php
7 months ago
BearerTokenTrait.php
4 months ago
BenchmarkTrait.php
2 years ago
BooleanTransientTrait.php
5 years ago
DatabaseSearchReplaceTrait.php
2 weeks ago
DbRowsGeneratorTrait.php
3 years ago
DebugLogTrait.php
1 year ago
DeveloperTimerTrait.php
9 months ago
EndOfLinePlaceholderTrait.php
1 year ago
EventLoggerTrait.php
1 month ago
FileScanToCacheTrait.php
10 months ago
FormatTrait.php
11 months ago
HttpRequestTrait.php
9 months ago
HydrateTrait.php
1 year ago
I18nTrait.php
1 year ago
IpResolverTrait.php
10 months ago
MaintenanceTrait.php
5 months ago
MemoryExhaustTrait.php
2 years ago
MySQLRowsGeneratorTrait.php
7 months ago
NoticesTrait.php
1 day ago
PropertyConstructor.php
5 years ago
RenameTmpDirectoryTrait.php
5 months ago
ResourceTrait.php
4 months ago
RestRequestTrait.php
3 months ago
RestoreFileExclusionTrait.php
1 year ago
SerializeTrait.php
1 year ago
SetTimeLimitTrait.php
1 month ago
SlashTrait.php
1 year ago
SqlCommentTrait.php
3 months ago
TablePrefixValidator.php
3 months ago
UrlTrait.php
1 year ago
ValueGetterTrait.php
1 year ago
WindowsOsTrait.php
1 year ago
HttpRequestTrait.php
67 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Traits; |
| 4 | |
| 5 | use WPStaging\Backup\Exceptions\StorageException; |
| 6 | |
| 7 | trait HttpRequestTrait |
| 8 | { |
| 9 | /** |
| 10 | * @param string $url. |
| 11 | * @param array $args Optional. Request arguments. Default empty array. |
| 12 | * See https://developer.wordpress.org/reference/classes/WP_Http/request/ for information on accepted arguments. |
| 13 | * @param bool $decodeBody Optional. If true the body will be decoded using json_decode. |
| 14 | * |
| 15 | * @throws StorageException |
| 16 | * @return string|array By default the request's response body will be decoded and return as array, |
| 17 | * to return the response body as string, pass the decodeBody param as false. |
| 18 | */ |
| 19 | protected function getRequestBody(string $url, array $args = [], bool $decodeBody = true) |
| 20 | { |
| 21 | $response = $this->getRemoteRequest($url, $args); |
| 22 | $body = wp_remote_retrieve_body($response); |
| 23 | if ($decodeBody) { |
| 24 | return json_decode($body, true); |
| 25 | } |
| 26 | |
| 27 | return $body; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * @param string $url. |
| 32 | * @param array $args Optional. Request arguments. Default empty array. |
| 33 | * See https://developer.wordpress.org/reference/classes/WP_Http/request/ for information on accepted arguments. |
| 34 | * |
| 35 | * @throws StorageException |
| 36 | * @return array The response array |
| 37 | */ |
| 38 | protected function getRemoteRequest(string $url, array $args = []): array |
| 39 | { |
| 40 | $defaults = [ |
| 41 | 'timeout' => 40, |
| 42 | 'httpversion' => '1.0', |
| 43 | 'sslverify' => false, |
| 44 | 'method' => 'GET', |
| 45 | ]; |
| 46 | $args = wp_parse_args($args, $defaults); |
| 47 | $response = wp_remote_request($url, $args); |
| 48 | $responseCode = wp_remote_retrieve_response_code($response); |
| 49 | |
| 50 | if (is_wp_error($response) || (!in_array($responseCode, [200, 201, 202, 204, 206, 302, 308]))) { |
| 51 | $errorMessage = is_wp_error($response) ? $response->get_error_message() : wp_remote_retrieve_body($response); |
| 52 | |
| 53 | $xml = @simplexml_load_string($errorMessage); |
| 54 | if ($xml !== false) { |
| 55 | $errorMessage = (string)$xml->Message ?? (string)$xml->message ?? $errorMessage; |
| 56 | if (!empty((string)$xml->Code) || !empty((string)$xml->code)) { |
| 57 | $errorMessage .= " (Code: " . ((string)$xml->Code ?? (string)$xml->code) . ")"; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | throw new StorageException("Error Message: $errorMessage; Error Code: $responseCode; Url: $url", (int)$responseCode); |
| 62 | } |
| 63 | |
| 64 | return $response; |
| 65 | } |
| 66 | } |
| 67 |