file_get_contents causing my server to hang
So I have my blogs running OK on my current VPS server, but decided to start migrating to a new dedicated server and that's where my troubles began.
I noticed my pages hanging badly, yet they only happened on my blogs, not on static pages. I then realized they don't hang when I go to the wp-admin section, which allowed me to troubleshoot file_get_contents as the problem. Once removed from my theme, the blog loaded perfectly.
So, I'm not sure what to do. My server currently has APF Firewall, but I loaded into SSH and temporarily disabled it. It was running CSF, but my management company swapped to APF to see if that changes it.
With CSF running, it didn't work. APF running, still didn't work. APF disabled, didn't work.
I'm not even sure it's the firewall at this point, but I don't know what else it开发者_运维百科 could be. allow_url_fopen is set to 'on' in the php.ini, but am I missing something? What else allows a server to access remote files?
Like I said, this worked ok on my VPS server, so the files worked, I'm just at a loss what could be causing the hangup.
Any suggestions? Thanks!
Try keeping a timeout on file_get_contents.
// set socket timeout
ini_set('default_socket_timeout', 60);
// read page contents
$response = file_get_contents("http://www.yoursite.com");
If you are making a request to an offsite domain (eg www.google.com) you may find the slow-down is due to a slow DNS lookup. If you are making a request like this I have a few suggestions:
- Test it outside of php: Do something like a
wget
orcURL
request via ssh to the same URL and see if it takes a while to resolve before it downloads. If it does it would generally indicate you had a network problem. - Cache the response: If the data you are requesting doesn't change often, look to implement caching of the response to a file or database. This will reduce the load time for your users, as well as a general performance boost. This can be as simple as writing the contents to a file on the local disk and updating it every so often.
It seems odd that a Wordpress theme would be making calls to file_get_contents
. I would investigate exactly what it is looking for and confirm you actually want it to load the remote file in.
精彩评论