Pages with Facebook Comments Are Very Slow

November 01, 2014
Karen

I recently moved a website and found that even though everything appeared to be set up exactly the same on the new server as it was on the old one, some pages were extremely slow to load. The only difference was that the new web server has a native IPv6 connection. Surprisingly, the culprit was using file_get_contents to load data from the Facebook Graph API.

PHP is trying to get a file from graph.facebook.com. Facebook have set DNS A records (IPv4) as well as AAAA (IPv6) records:

$ host graph.facebook.com
graph.facebook.com is an alias for api.facebook.com.
api.facebook.com is an alias for star.c10r.facebook.com.
star.c10r.facebook.com has address 173.252.100.27
star.c10r.facebook.com has IPv6 address 2a03:2880:2110:cf07:face:b00c::1

However, the server at the end of the AAAA record, i.e. the IPv6 enabled server does not understand the URI http://graph.facebook.com/comments/?ids=xyz… but instead of returning something useful like a 500 error it just hangs for ages. This is not a problem for most people because even though anyone can look up an AAAA record, only IPv6 enabled web servers will attempt to connect using IPv6.

So, for IPv6 connected hosts, file_get_contents just hangs at this point and stops the rest of the page from loading. The possible fixes are to either disable IPv6 on the web server running the PHP script (a bit drastic!), or to use curl because it has the capability to ignore IPv6 entirely using the following option:

curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );

Hope this saves somebody some head scratching anyway!