admin管理员组

文章数量:1322850

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.

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.

Share Improve this question edited Jun 8, 2019 at 1:53 Matthew Brown aka Lord Matt asked Jun 2, 2019 at 18:03 Matthew Brown aka Lord MattMatthew Brown aka Lord Matt 1,0683 gold badges13 silver badges34 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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';
$cookies[] = $cookie;

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

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

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

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/WordPress/WordPress/blob/f547eb617441853b6e316c6f127a2182c12925e3/wp-includes/class-http.php#L775-L797

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

本文标签: wp remote getPassing cookies when using wpremoteget