Javascript php中的file_get_contents()超时
我知道php没有提供超时选项,因为它是从服务器端运行的。但我的逻辑需要暂停。请解决这个案子 我的PHP:login.PHP:-Javascript php中的file_get_contents()超时,javascript,php,timeout,file-get-contents,Javascript,Php,Timeout,File Get Contents,我知道php没有提供超时选项,因为它是从服务器端运行的。但我的逻辑需要暂停。请解决这个案子 我的PHP:login.PHP:- $forms_values = $_POST; $url = "http://sample.com/sample?params=$forms_values"; $resp = file_get_contents($url); // It takes much time if sample.com is down. // so need to set time out h
$forms_values = $_POST;
$url = "http://sample.com/sample?params=$forms_values";
$resp = file_get_contents($url);
// It takes much time if sample.com is down.
// so need to set time out here.
$response = json_decode($resp, true);
....
....
if ($response["auth"] == "yes"){
header("index.php");
}
我的htmlindex.html:-
<form action="login.php" method="post" id="clientLogin">
<div>
<input id="username" placeholder="Email or Username" type="text" name="luser" size="30" value="">
</div>
<div>
<input id="passwd" placeholder="Password" type="password" name="lpasswd" size="30" value="">
</div>
<input class="btn" type="submit" value="Sign In">
</form>
<script type="text/javascript">
//This is not working. and obviously delay will happen in server code
//and i cant get it in client side
$(window).load(function () {
var submit = false;
$("#clientLogin").submit(function(e) {
alert("me after 1000 mili seconds");
setTimeout(function(){
alert("me after 1000 mili seconds");
submit = true;
$("#clientLogin").submit(); // if you want
}, 15000);
if(!submit)
e.preventDefault();
});
};
</script>
//这是行不通的。显然,服务器代码中会出现延迟
//我不能在客户端得到它
$(窗口)。加载(函数(){
var submit=false;
$(“#clientLogin”).submit(函数(e){
警报(“1000毫秒后提示我”);
setTimeout(函数(){
警报(“1000毫秒后提示我”);
提交=真;
$(“#clientLogin”).submit();//如果需要
}, 15000);
如果(!提交)
e、 预防默认值();
});
};
这个javascript不工作。很明显,服务器代码中会出现延迟,我无法从客户端观看
这就是我想要的。如果我的文件\u get\u contents()
函数长时间没有响应,则需要终止提交过程(从提交到呈现下一页)
注意:
请不要让我使用卷发,因为我被指示不要使用卷发。
我无法在客户端获取它文件\u get\u contents()中超时的默认值是60秒。 您可以通过默认的\u套接字\u超时设置更改此值。 这在您的代码中也是可行的:
ini_set('default_socket_timeout', 2); // 2 seconds
将代码放在脚本的开头
ini_set('default_socket_timeout', 30); // 30 seconds
默认设置为60秒。
< P>可选地,考虑使用.< /P> 它提供CURLOPT_CONNECTTIMEOUT参数,以指定尝试建立连接时等待的时间量 set_time_limit()确实全局运行,但可以在本地重置 设置允许脚本运行的秒数。如果达到该值,脚本将返回一个致命错误。默认限制为30秒,如果存在,则为php.ini中定义的max_execution_time值 调用时,set_time_limit()会从零重新启动超时计数器。换句话说,如果超时是默认的30秒,并且在脚本执行25秒后进行了诸如set_time_limit(20)之类的调用,则脚本将在超时之前总共运行45秒。 我还没有测试过它,但您可以在本地设置它,在离开服务器时重置它<?php
set_time_limit(0); // global setting
function doStuff()
{
set_time_limit(10); // limit this function
// stuff
set_time_limit(10); // give ourselves another 10 seconds if we want
// stuff
set_time_limit(0); // the rest of the file can run forever
}
// ....
sleep(900);
// ....
doStuff(); // only has 10 secs to run
// ....
sleep(900);
// ....
如果您在5秒内没有收到内容,$resp
将为空
$ctx=stream\u context\u create(['http'=>['timeout'=>5]]);//5秒
$resp=file\u get\u contents($url,null,$ctx);
如果超时,我的文件\u get\u contents()
将返回什么。因为我需要向客户端发送一些错误消息。它应该返回FALSE。使用===运算符比较返回值。感谢ini\u set()
和设置ini\u set('default\u socket\u timeout',60)
回到我的警告消息旁边将使其成为默认(60秒)仪式?是的。如果你想重置它,你会这样做。它正常并且ini\u设置('default\u socket\u timeout',2);
一个healped。谢谢你的回答我很感激。谢谢你的回答我很感激