WP_AI_Client_HTTP_Client::create_psr_response( $wp_response ): WordPressAiClientDependenciesPsrHttpMessageResponseInterface

In this article

This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only by core. It is listed here for completeness.

Creates a PSR-7 response from a WordPress HTTP response.

Parameters

mixed> $wp_response WordPress HTTP API response array.

Return

WordPressAiClientDependenciesPsrHttpMessageResponseInterface PSR-7 response.

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

VersionDescription
7.0.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.