Passing cookies when using wp_remote_get

I'm working on a plugin that fetches remote RSS feeds using wp_remote_get. The problem is that Tumblr requires a cookie for GDPR compliance. This includes RSS feeds. Which means all Tumblr feeds are currently broken.

I found an example of using CURL to acquire the cookies so the feed will load. However, I have little more than a passing acquaintance with wp_remote_get and related functions.

How do I implement the cookie getting hack from the example via WordPress' remote get methodology?

Edit: To clarify, I need to get and then use a cookie. That's two requests; one for the cookie and a second using the cookie.

Topic wp-remote-get cookies Wordpress

Category Web


Going off of @phatskat answer, you can also pass the values in the constructor as well, and example from WooCommerce is as follows:

$cookies = [];
$cookies[] = new WP_Http_Cookie( array(
    'name'  => $name,
    'value' => $value,
));


$args = [
    'cookies' => $cookies,
];

$response = wp_remote_get( $url, $args );

Looking at core code though, there is a method to build the cookies when a request is made (@since 2.8.0): https://github.com/WordPress/WordPress/blob/f547eb617441853b6e316c6f127a2182c12925e3/wp-includes/class-http.php#L775-L797

When CURL or STREAM request is made: https://github.com/WordPress/WordPress/blob/57a3f803ae67814b2639ab8816d59c5fa5add441/wp-includes/class-wp-http-curl.php#L93


The arguments array for wp_remote_get accepts a cookies parameter:

$cookies = [];
$cookie = new WP_Http_Cookie( 'cookie_name' );
$cookie->name = 'cookie_name';
$cookie->value = 'your cookie value';
$cookie->expires = 7 * DAY_IN_SECONDS; // expires in 7 days
$cookie->path = '/';
$cookie->domain = '.reddit.com';
$cookies[] = $cookie;

$url = 'https://tumblr.com/some/url/';

$args = [
    'cookies' => $cookies,
];

$response = wp_remote_get( $url, $args );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.