wp_remote_retrieve_cookies()
Retrieves all data of all cookies from the given response to a request.
Used By: wp_remote_retrieve_cookie()
No Hooks.
Returns
WP_Http_Cookie[].
Arrayof WP_Http_Cookie objects.Empty array, if there are no cookies or the response contains a WP_Error.
Usage
wp_remote_retrieve_cookies( $response );
- $response(array) (required)
- Response to a request obtained by one of the functions: wp_remote_*()
Examples
#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() wp remote retrieve cookies code WP 7.0
function wp_remote_retrieve_cookies( $response ) {
if ( is_wp_error( $response ) || empty( $response['cookies'] ) ) {
return array();
}
return $response['cookies'];
}