InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Fetching a URL with a Timeout in PHP
PHP

Fetching a URL with a Timeout in PHP

InfinityCoder December 20, 2016

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:

1
2
3
// 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:

1
2
3
4
5
$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:

1
2
3
4
5
6
7
8
require_once 'HTTP/Request2.php';
 
$r = new HTTP_Request2('http://slow.example.com/');
$r->setConfig(array(
    'connect_timeout' => 15
));
 
$page = $r->send()->getBody();

Remote servers are fickle beasts. Even the most most robust, enterprise-class, missioncritical 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:

1
2
3
4
$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:

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

1
2
3
4
5
6
7
8
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.

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Finding the Difference of Two Dates in PHP
    You want to find the elapsed time between …

Finding the Difference of Two Dates in PHP

Parsing Program Arguments in PHP
You want to process arguments passed on the command line. …

Parsing Program Arguments in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap