Recipe 13.6. Fetching a URL with a Timeout


13.6.1. Problem

You want to fetch a remote URL, but don't want to wait around too long if the remote server is busy or slow.

13.6.2. Solution

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

<?php // 15 second timeout ini_set('default_socket_timeout', 15); $page = file_get_contents('http://slow.example.com/'); 

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

 <?php $c = curl_init('http://slow.example.com/'); curl_setopt($c, CURLOPT_RETURNTRANSFER, true); curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 15); $page = curl_exec($c); curl_close($c); ?>

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

<?php require_once 'HTTP/Request.php'; $opts = array('timeout' => 15); $req = new HTTP_Request('http://slow.example.com/', $opts); $req->sendRequest(); ?>

13.6.3. Discussion

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

<?php $url = 'http://slow.example.com'; $stream = fopen($url, 'r'); stream_set_timeout($stream, 20); $response_body = stream_get_contents($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

<?php require_once 'HTTP/Request.php'; $opts = array('readTimeout' => array(20,0)); $req = new HTTP_Request('http://slow.example.com/', $opts); $req->sendRequest(); ?>

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.




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net