php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #36072 stream_set_connection_timeout()
Submitted: 2006-01-18 21:16 UTC Modified: 2010-09-27 16:13 UTC
Votes:3
Avg. Score:4.3 ± 0.9
Reproduced:3 of 3 (100.0%)
Same Version:1 (33.3%)
Same OS:0 (0.0%)
From: ceo at l-i-e dot com Assigned:
Status: Open Package: Streams related
PHP Version: * OS: *
Private report: No CVE-ID: None
 [2006-01-18 21:16 UTC] ceo at l-i-e dot com
Description:
------------
I love the simplicity of fopen() that takes most URLs / files / whatever and does what I want.

Do whatever it takes to start sending me the data.

I NEED the ability to set the time-out for the opening, as well as the time-out after the stream has opened.

I'm stuck with duplicating whatever code is down in fopen() in my own PHP code to detect and initiate protocol specific minutia because fopen() has no user-configurable timeout, but fsockopen doesn't do all that.
$parts = parse_url($url);
extract($parts);
switch($scheme){
case 'http': fputs("GET $path HTTP/1.0\n"); fputs("Host: $host\n"); break;
case 'ftp': fputs("GET $path\n"); break;
.
.
.
}

I REALLY don't want to re-invent the wheel here, when I know that code is down in the guts of fopen()

A function stream_set_connection_timeout() to let me tell PHP how long fopen() should wait would make life way more better for many users, I believe.


Expected result:
----------------
The PHP Dev Team is going to add this function because YOU ROCK!
:-)



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-01-18 21:24 UTC] helly@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

RTFM: stream_set_timeout(), context of fopen(), streams section in manual
 [2006-01-19 00:31 UTC] ceo at l-i-e dot com
Errrr. If you read the actual Feature Request, I think it's pretty clear that RTFM stream_set_timeout won't cut it...

stream_set_timeout is useful only AFTER a stream is open.

fopen() gives no control (tho fsockopen() does) over connection timeout.

But fsockopen is missing all the great stuff in fopen() that takes care of.

I really don't think I'm being an idiot here...  Honest.
 [2010-04-12 02:36 UTC] pajoye@php.net
-Package: Feature/Change Request +Package: Streams related
 [2010-09-27 16:13 UTC] cataphract@php.net
You can already do this for http/https:


<?php
$context = stream_context_create(
	array(
		"http" => array("timeout"=>2),
));
var_dump(fopen("http://136.234.1.4/", "r", false, $context));

For FTP, there isn't unfortunately a way, by you can set a default timeout:

ini_set("default_socket_timeout", 2);
 [2012-07-20 22:41 UTC] colin at mollenhour dot com
I think an excellent solution would be to add a "timeout" option to the Socket 
stream options. The HTTP stream already supports timeout, but not regular sockets.  
For example, I want to connect to a Redis server using tcp for caching (infinite 
timeout needed for blocking calls) and may want to connect to a different server 
for some other purpose with a short timeout. Example:

$context = stream_context_create(array('socket' => array('timeout' => 5)));
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 14:01:29 2024 UTC