Creates a PSR-7 response from a WordPress HTTP response.
Parameters
- mixed> $wp_response WordPress HTTP API response array.
Source
private function create_psr_response( array $wp_response ): ResponseInterface {
$status_code = wp_remote_retrieve_response_code( $wp_response );
$reason_phrase = wp_remote_retrieve_response_message( $wp_response );
$headers = wp_remote_retrieve_headers( $wp_response );
$body = wp_remote_retrieve_body( $wp_response );
$response = $this->response_factory->createResponse( (int) $status_code, $reason_phrase );
if ( $headers instanceof WP_HTTP_Requests_Response ) {
$headers = $headers->get_headers();
}
if ( is_array( $headers ) || $headers instanceof Traversable ) {
foreach ( $headers as $name => $value ) {
$response = $response->withHeader( $name, $value );
}
}
if ( ! empty( $body ) ) {
$stream = $this->stream_factory->createStream( $body );
$response = $response->withBody( $stream );
}
return $response;
}
Changelog
| Version | Description |
|---|---|
| 7.0.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.