Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
处理ajax到php的长请求_Php_Jquery_Ajax_Multithreading_Curl - Fatal编程技术网

处理ajax到php的长请求

处理ajax到php的长请求,php,jquery,ajax,multithreading,curl,Php,Jquery,Ajax,Multithreading,Curl,我正在开发一个web应用程序,其中用户单击一个按钮来启动对php文件的ajax请求。php文件需要很长时间才能运行,这迫使用户在通知用户请求已完成之前等待php文件完成运行 下面是我正在使用的代码示例: jQuery调用者: $('#button').click(function(){ $.ajax({ type: "POST", url: 'index-process.php', success: function (data) { conso

我正在开发一个web应用程序,其中用户单击一个按钮来启动对php文件的ajax请求。php文件需要很长时间才能运行,这迫使用户在通知用户请求已完成之前等待php文件完成运行

下面是我正在使用的代码示例:

jQuery调用者:

$('#button').click(function(){
 $.ajax({
    type: "POST",
    url: 'index-process.php',
    success: function (data) {
            console.log(data);
            alert('finished');
    }
 });
});
index-process.php

<?php

/// placeholder for a long script
sleep(60);
echo "finished processing";

?>
<?php

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'index-process2.php');
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'do_nothing');
curl_exec($ch);  
curl_close($ch);
echo "finished processing";

function do_nothing($curl, $input) {
    return 0; // aborts transfer with an error
}
?>

我正在寻找一种可以让我通知用户请求已提交的解决方法。然后让代码在后台完成运行。我不一定需要我的php脚本来向用户返回任何值。它只需要执行

到目前为止,我已经尝试过使用两个不同php文件的curl请求执行类似的操作,但它仍然会强制用户在完成ajax请求之前等待两个php文件都已运行完毕:

index-process.php

<?php

/// placeholder for a long script
sleep(60);
echo "finished processing";

?>
<?php

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'index-process2.php');
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'do_nothing');
curl_exec($ch);  
curl_close($ch);
echo "finished processing";

function do_nothing($curl, $input) {
    return 0; // aborts transfer with an error
}
?>

index-process2.php

<?php
ob_end_clean();
ignore_user_abort(true);
ob_start();
header("Connection: close");
header("Content-Length: " . ob_get_length());
ob_end_flush();
flush();

/// placeholder for a long script
sleep(60);
?>


如果使用php没有一个好的解决方案,那么使用jQuery有可能吗?如果是这样,有人能告诉我正确的编码方法吗?

fsockopen
是我能想到的最好的答案。希望这对以后的人有所帮助。下面的代码允许我通过ajax调用file1.php,它将数据发送到file2.php。一旦file1.php使用fsockopen将数据发送到file2.php,它就不会等待响应。当file2.php正在运行时,可以在file1.php中运行其他代码。即使file2.php仍在运行,File1.php也可以立即响应ajax请求

这个答案只对需要运行长时间执行的脚本的人有用,该脚本需要输入数据,但不需要返回数据

通过ajax请求调用file1.php:

$vars = array('hello' => 'world');
$post_data = http_build_query($vars);

/// ssl and 443 are used for https, change to tls and 80 for http
/// only the main website domain is needed
/// do not put the full path to the file you need to call here 
$fp = fsockopen("ssl://www.main-website-domain.com", 443, $errno, $errstr, 1);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    //// this is where the full path to the file you want to reach goes
    //// format is (method) (path not including the domain) (HTTP version)
    $out = "POST /Full/Path/to-test-file2.php HTTP/1.1\r\n";
    $out .= "Host: www.main-website-domain.com\r\n";
    $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
    $out .= "Content-length: " . strlen($post_data) . "\r\n";
    $out .= "User-Agent: What-ever-you-want\r\n";
    $out .= "Connection: close\r\n\r\n";
    $out .= $post_data . "\r\n\r\n";
    fwrite($fp, $out);
    fclose($fp);
}

对于使用
php fpm
(意思是通过
mod_fcgi
使用
nginx
apache
)的人,可以使用来实现所描述的行为

它做什么
fastcgi\u finish\u请求
将输出发送到客户端(浏览器),但它将执行函数后指定的所有代码

例子
你能详细说明一下你到底想要什么吗?你不需要两个PHP文件。只需在ajax请求上设置1秒超时,然后让
ignore\u user\u abort(true)。ajax请求将终止,PHP将继续运行。不太确定。我在共享服务器上。我还没试过