You want to fetch a remote URL, but don't want to wait around too long if the remote server is busy or slow.
With the http stream, set the default_socket_timeout configuration option. Example 13-28 waits no more than 15 seconds to establish the connection with the remote server.
Setting a timeout with the http stream
Note that changing default_socket_timeout affects all new sockets or remote connections created in a particular script execution.
With cURL, set the CURLOPT_CONNECTTIMEOUT option, as shown in Example 13-29.
Setting a timeout with cURL
With HTTP_Request, set the timeout element in a parameter array passed to the HTTP_Request constructor, as shown in Example 13-30.
Setting a timeout with HTTP_Request
Remote servers are fickle beasts. Even the most most robust, enterprise-class, mission-critical service can experience an outage. Alternatively, a remote service you depend on can be up and running, but be unable to handle your requests because of network problems between your server and the remote server. Limiting the amount of time that PHP waits to connect to a remote server is a good idea if using data from remote sources is part of your page construction process.
All of the techniques outlined in the Solution limit the amount of time PHP waits to connect to a remote server. Once the connection is made, though, all bets are off in terms of response time. If you're truly concerned about speedy responses, additionally set a limit on how long PHP waits to receive data from the already connected socket. For a stream connection, use the stream_set_timeout( ) function. This function needs to be passed a stream resource, so you have to open a stream with fopen( )'no file_get_contents( ) here. Example 13-31 limits the read timeout to 20 seconds.
Setting the read timeout with the http stream
With cURL, set the CURLOPT_TIMEOUT to the maximum amount of time curl_exec( ) should operate. This includes both the connection timeout and the time to read the entire response body.
With HTTP_Request, add a readTimeout value to the parameter array you pass to the constructor. This value must be a two-element array of seconds and microseconds. Example 13-32 sets the read timeout to 20 seconds.
Setting a read timeout with HTTP_Request
Although setting connection and read timeouts can improve performance, it can also lead to garbled responses. Your script could read just a partial response before a timeout expires. If you've set timeouts, be sure to validate the entire response that you've received. Alternatively, in situations where fast page generation is crucial, retrieve external data in a separate process and write it to a local cache. This way, your pages can use the cache without fear of timeouts or partial responses.
13.6.4. See Also
Documentation on curl_setopt( ) at http://www.php.net/curl-setopt, on stream_set_timeout( ) at http://www.php.net/stream_set_timeout, on default_socket_timeout at http://www.php.net/filesystem, and on the PEAR HTTP_Request class at http://pear.php.net/package/HTTP_Request.