I had the same problem before. I posted a question on here and no body gave me an answer. After a few tries, I figured it out and it was infront of me the whole time. It's the socket_set_timeout() or the stream_set_timeout() functions.
Example:
$fp = fopen(...);
stream_set_timeout($fp,2); // set 2 second limit
fread($fp,...); // if 2 seconds pass without results, statement ends
Enjoy