What is this code trying to do? It was the cause of my 20s TTFB

Most probably it’s some kind of malicious code.

What it does is:

  1. Disable error reporting
  2. Check the available methods of getting contents from remote server
  3. Send a request to a remote server
  4. Display the result of that request on your site

So it’s some kind of spammy SEO links or something like that.

And no, your site shouldn’t have such code on it and if it does – most probably it’s a result of malware infection.