Curious handling of timeout by fsockopen??
We’ve been finding that sometimes my code using fsockopen doesn’t appear to be timing out in the given timeout period and I think I’ve tracked down the reason…
Essentially what’s been happening is that if the remote url is accessible (i.e. an fsockopen connection can be made to it), but the remote url takes a long time to respond, then the connection will just hang until a response is given – even if this exceeds the given timeout setting.
So if I have my timeout set to 10 seconds, but the remote URL takes 15 seconds to give a response, the script will NOT timeout after 10 seconds, instead it will wait until the remote URL responds. So my impression is that the timeout setting is only a connection timeout, not a general timeout – which was contrary to how I thought the timeout setting functioned. Thinking about if after this kinda makes sense, as the timeout is only on opening the connection, not on fopen – so it can’t know the remote url is taking longer than expected to respond.
As I can’t see a way to set a ‘real’ timeout on an fsock or fopen connection, I had a go using the cURL library. This appears to work in the way I expected – i.e. setting a timeout of 10 seconds would mean the script would stop after 10 seconds as the remote URL had not responded.










