比如
执行a代码如果在5秒钟内没有完成
就自动中断执行a代码,转向执行b代码
有人说用set_time_limit这个函数是可以超时显示错误信息
但我要的是超时转向其他操作
而不是输出错误信息请各位帮帮忙!
执行a代码如果在5秒钟内没有完成
就自动中断执行a代码,转向执行b代码
有人说用set_time_limit这个函数是可以超时显示错误信息
但我要的是超时转向其他操作
而不是输出错误信息请各位帮帮忙!
调试欢乐多
register_shutdown_function( "time_out_callback");
set_time_limit( 3 );//假设3秒超时
error_reporting( 0 );//屏蔽fatal error
//模拟超时
while (true) {
//echo connection_status()."<br/>";
}
error_reporting($es);//恢复设置function time_out_callback()
{
if(connection_status() == 2)
{
b();
}
}
function b()
{
echo 'hello world!';
}
你把while循环里那个注释去掉
可以看到连接状态一直是0(CONNECTION_NORMAL)
一执行里面就跳不出来了
register_shutdown_function( "time_out_callback");
set_time_limit( 3 );//假设3秒就算超时
error_reporting( 0 );//屏蔽fatal error
//模拟超时
c();
error_reporting($es);//恢复设置function time_out_callback()
{
if(connection_status() == 2)
{
b();
}
}
function c()
{
//你的超时代码
while (true) {
}
}
function b()
{
echo 'hello world!';
}
我执行这段东西是3个0,一个2,你呢?
$es = ini_get('error_reporting');
register_shutdown_function( "time_out_callback");
set_time_limit( 3 );//假设3秒就算超时
error_reporting( 0 );//屏蔽fatal error
//模拟超时
c();
error_reporting($es);//恢复设置function time_out_callback()
{
if(connection_status() == 2)
{
b();
}
}
function c()
{
//你的超时代码
while (true) {
echo connection_status()."<br/>";//echo 0
sleep( 1 );//给cpu透口气
}
}
function b()
{
echo connection_status();//echo 2,c超时
}
那里面我给他 ECHO 返回错误信息之类的
$ctx = stream_context_create(array(
'http' => array(
'timeout' => 1
)
)
);
file_get_contents("http://example.com/", 0, $ctx);
?>