I recently upgraded the forum to 2.3.1 (+ to PHP7) and now have issues with the code quoted below.
This code grabs the latest discussions, caches them and displays on a non-forum page but now I have over a thousand exception errors in bugsnag for it, "HTTP request failed! HTTP/1.1 403 Forbidden". See screenshot.
I can't help you with this specific question, but I wonder about the approach that does directly to the data - why are you not using Vanilla's ability to create an RSS feed (possibly with the aid of the RSS Options plugin) and just grab the feed's result into whatever destination you needed?
I think at the time I preferred the flexibility of displaying and caching exactly how I liked rather than using third-party plugins. This was a couple of years ago so not sure how things have changed.
Instead of using file_get_contents you should try curl.