wp_remote_retrieve_cookies()WP 4.4.0

Retrieve only the cookies from the raw response.

No Hooks.

Return

WP_Http_Cookie[]. An array of WP_Http_Cookie objects from the response. Empty array if there are none, or the response is a WP_Error.

Usage

wp_remote_retrieve_cookies( $response );
$response(array|WP_Error) (required)
HTTP response.

Examples

0

#1 Get the data of all remote cookies

Suppose we send a request and the response contains cookies. Let's get all the data about all the cookies in the response.

To create such a response with cookies as a demonstration, let's use the service httpbin.org, which allows us to set cookies in the response we receive from the server.

$url = 'https://httpbin.org/cookies/set';

// let's add a request to return a cookie in the response
$url .= '?mycookie=value1&mycook=value2';

$response = wp_remote_get( $url );

print_r( wp_remote_retrieve_cookies($response) );

/*
Array
(
	[0] => WP_Http_Cookie Object
		(
			[name] => mycookie
			[value] => value1
			[expires] => 
			[path] => /
			[domain] => httpbin.org
		)

	[1] => WP_Http_Cookie Object
		(
			[name] => mycook
			[value] => value2
			[expires] => 
			[path] => /
			[domain] => httpbin.org
		)

)
*/

Changelog

Since 4.4.0 Introduced.

wp_remote_retrieve_cookies() code WP 6.5.4

function wp_remote_retrieve_cookies( $response ) {
	if ( is_wp_error( $response ) || empty( $response['cookies'] ) ) {
		return array();
	}

	return $response['cookies'];
}