So it looks like I’ve managed to fix my own problem here. I don’t know if this is the best thinkable solution to this exact problem, but it does work!
My assumptions about CURLOPT_FOLLOWLOCATION
seem correct. As Sally CJ also explained in the comments, the default setting in WP is always set to false
. That’s why the data always contained contents from the (302) redirect instead of the actual targeted URL.
I’ve found this old thread and answer where the underlying CURLOPT_FOLLOWLOCATION
is changed through the http_api_curl
action. Using this action to override the default setting was the key for fixing my problem with wp_remote_get
. With the function enabled (see below), my returned data is exactly the same as when using cURL.
function __set_curl_to_follow( &$handle ) {
curl_setopt( $handle, CURLOPT_FOLLOWLOCATION, true );
}
add_action( 'http_api_curl', '__set_curl_to_follow' );
Special thanks to Sally CJ and bosco for thinking with me on this!