ssl_verify = $ssl_verify; $this->timeout = $timeout; $this->retries = $retries; } /** * Do a GET request to retrieve the contents of a remote URL. * * @param string $url URL to get. * @param array $headers Optional. Associative array of headers to send with the request. Defaults to empty array. * @return Response Response for the executed request. * @throws FailedToGetFromRemoteUrl If retrieving the contents from the URL failed. */ public function get( $url, $headers = [] ) { $retries_left = $this->retries; do { $args = [ 'method' => 'GET', 'timeout' => $this->timeout, 'sslverify' => $this->ssl_verify, 'headers' => $headers, ]; $response = wp_remote_get( $url, $args ); if ( $response instanceof WP_Error ) { return new RemoteGetRequestResponse( $response->get_error_message(), [], 500 ); } if ( ! isset( $response['response']['code'] ) ) { return new RemoteGetRequestResponse( isset( $response['response']['message'] ) ? $response['response']['message'] : 'Unknown error', [], 500 ); } $status = isset( $response['response']['code'] ) ? $response['response']['code'] : 500; if ( $status < 200 || $status >= 300 ) { if ( ! $retries_left || in_array( $status, self::RETRYABLE_STATUS_CODES, true ) === false ) { throw FailedToGetFromRemoteUrl::withHttpStatus( $url, $status ); } continue; } $headers = $response['headers']; if ( $headers instanceof Traversable ) { $headers = iterator_to_array( $headers ); } if ( ! is_array( $headers ) ) { $headers = []; } return new RemoteGetRequestResponse( $response['body'], $headers, (int) $status ); } while ( $retries_left-- ); // This should never be triggered, but we want to ensure we always have a typed return value, // to make PHPStan happy. return new RemoteGetRequestResponse( '', [], 500 ); } }