1

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.

2 Answers 2

2

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 );
2
  • Does that work for getting the cookie first? If so, how do I nab that? Commented Jun 6, 2019 at 21:50
  • 1
    Sorry, I think I misunderstood your request a little. To get the cookies from a wp_remote_request method such as wp_remote_get or wp_remote_post, you can access the cookies key of the returned array. e.g. $r = wp_remote_get( <url> ); print_r( $r['cookies'] );
    – phatskat
    Commented Jun 19, 2019 at 16:44
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.