For some reason file_get_contents() fails intermittently on some websites that support IPv6 (maybe 20% of the time, the command times-out). Although disabling IPv6 resolves the issue, that is not the best solution as more sites move to IPv6.
I wrote a simple PHP gethtml() function that works around the issue using wget. This will automatically use IPv6 when IPv4 is not available. A minor drawback is that it uses an external command, but I think it is preferable to disabling IPv6.
If wget is not already installed on your distribution, you can install it as follows:
sudo apt install wget
PHP Function Example:
$myhtml = gethtml("http://example.com");
//use instead of file_get_contents() due to inconsistent IPv6 performance
function gethtml($url){return shell_exec("wget --prefer-family=IPv4 -qO- ".$url);}