PHP Consuming RESTful APIs Fetching a URL with a Timeout - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP Consuming RESTful APIs Fetching a URL with a Timeout - Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Wednesday, June 19, 2019

PHP Consuming RESTful APIs Fetching a URL with a Timeout

PHP Consuming RESTful APIs




Fetching a URL with a Timeout


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.

Solution

With the http stream, set the default_socket_timeout configuration option:

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

This waits up to 15 seconds to establish the connection with the remote server. Changing default_socket_timeout affects all new sockets or remote connections created in a particular script execution.

With cURL, set the CURLOPT_CONNECTTIMEOUT option:

       $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_Request2, set the timeout element in a parameter array passed to the HTTP_Request2 constructor:

       require_once 'HTTP/Request2.php';
    
       $r = new HTTP_Request2('http://slow.example.com/');
       $r->setConfig(array(
                'connect_timeout' => 15
       ));

       $page = $r->send()->getBody();

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. This example limits the read timeout to 20 seconds:

       $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:

       curl_setopt($c, CURLOPT_TIMEOUT, 35);

With HTTP_Request2, add a timeout value to the configuration array. This value is the number of seconds. Here it’s 20 seconds:

       require_once 'HTTP/Request2.php';

       $r = new HTTP_Request2('http://slow.example.com/');
       $r->setConfig(array(
                'timeout' => 20
       ));

       $page = $r->send()->getBody();

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.


No comments:

Post a Comment

Post Top Ad