php打开一个异步请求,并允许客户端检查请求是否已完成

php打开一个异步请求,并允许客户端检查请求是否已完成,php,asynchronous,Php,Asynchronous,我正在尝试通过php创建一个异步请求,它将允许我检查状态(完成或仍在运行),完成请求大约需要6-20分钟。 我需要代码独立于系统并维护请求头代理id(因为这用于验证我的用户),所以我想我可以按照问题的第二个答案做一些事情 问题是,我看不到一种与服务器通信的方法来验证在请求被触发后操作是否已完成。有什么简单的方法可以做到这一点吗?(我无法预测事件的影响,aka值可能会也可能不会因此而改变)因此,Christian Davénpost在第页激发了我对自己问题的解决方案,如下所示: if(array_

我正在尝试通过php创建一个异步请求,它将允许我检查状态(完成或仍在运行),完成请求大约需要6-20分钟。 我需要代码独立于系统并维护请求头代理id(因为这用于验证我的用户),所以我想我可以按照问题的第二个答案做一些事情


问题是,我看不到一种与服务器通信的方法来验证在请求被触发后操作是否已完成。有什么简单的方法可以做到这一点吗?(我无法预测事件的影响,aka值可能会也可能不会因此而改变)

因此,Christian Davénpost在第页激发了我对自己问题的解决方案,如下所示:

if(array_key_exists('async', $_GET)){
   //short-task spawns the child(fakeLongTask())
   testing::curl_request_async();
}else{
    //short task, aka child task
    testing::fakeLongTask();
}

class testing{
    /**
     * asynch trigger a event(parent/short Task)
     */
    public static function curl_request_async(){
        //set file path to a unique file name for you...maybe consider using tmp type functionality
            $file = 'C:\\filepath\\logFile.txt';
        $fp = curl_init('http://localhost/testing.php?file='.$file);
            //send the request off
        curl_exec($fp);
            //because I am not sure that the socket is closed when it interperets the header, and because its a best practice
        curl_close($fp);

        while(file_exists($file)){
            echo 'we are waiting because the process is still going and the lock is still imposed.<br />'.PHP_EOL;
            sleep(3);
        }
    }

    /**
     * this is to mimic a long task that we might be running.
     * basic setup is clear the buffer, set the raw header to the close value to let the browser
     * know that the request has 'finished' so it can return to the calling function after the output is sent.
     * specify the size of the information being sent as this is required for the header. Then send the buffer and start
     * processing asynchronously
     * 
     * child/long/orphaned Task
     */
    public static function fakeLongTask(){
        //clear the buffer
        while(ob_get_level()){
             ob_end_clean();
        }
        //specify that this is a non-persistant connection
        header('Connection: close');
        //if the user kills the script ignore there request and keep on, nice feature to avoid early termination
        ignore_user_abort(true);
        ob_start();
        //if you need to send any information to the user specify that here
        $size = ob_get_length();
        header("Content-Length: $size");
        //since flush might not get everything we must do a 'power flush'
        ob_end_flush();
        flush();
        //from this point on the original function that had the curl op in it will continue and this will also continue on asynchronously
        //create the new file(this is our only way to communicate to the parent script as this function is now running rouge.
        $file = $_GET['file'];
        $fileHandle = fopen($file, 'w') or die('');
        fclose($fileHandle);
        //now that we have our file lets do our process, or in this case just stall for 10 seconds
        sleep(10);
        //we are done with our task delete the file
        unlink($file);
    }
}
if(数组\u键\u存在('async',$\u GET)){
//短任务生成子任务(fakeLongTask())
测试::curl_request_async();
}否则{
//短任务,又名子任务
测试::fakeLongTask();
}
类测试{
/**
*异步触发事件(父/短任务)
*/
公共静态函数curl\u request\u async(){
//为您设置唯一文件名的文件路径…可能考虑使用TMP类型功能
$file='C:\\filepath\\logFile.txt';
$fp=curl\u init('http://localhost/testing.php?file=“.$文件);
//发出请求
curl_exec($fp);
//因为我不确定套接字在解释标头时是否关闭,并且因为这是一种最佳实践
卷曲关闭($fp);
while(文件存在($file)){
echo“我们正在等待,因为进程仍在进行,锁仍在施加。”;
睡眠(3);
}
}
/**
*这是为了模拟我们可能正在运行的长任务。
*基本设置清除缓冲区后,将原始标题设置为关闭值以允许浏览器
*知道请求已“完成”,以便在发送输出后返回调用函数。
*指定要发送的信息的大小,因为这是标头所需的。然后发送缓冲区并启动
*异步处理
* 
*子任务/长任务/孤立任务
*/
公共静态函数fakeLongTask(){
//清除缓冲区
while(ob_get_level()){
ob_end_clean();
}
//指定这是一个非持久性连接
标题(“连接:关闭”);
//如果用户终止了脚本,则忽略该请求并继续,这是一个很好的功能,可以避免提前终止
忽略用户中止(true);
ob_start();
//如果需要向用户发送任何信息,请在此处指定
$size=ob_get_length();
标题(“内容长度:$size”);
//因为同花顺可能不会得到所有东西,所以我们必须做一次“强力同花顺”
ob_end_flush();
冲洗();
//从这一点开始,包含curl op的原始函数将继续,这也将异步继续
//创建新文件(这是我们与父脚本通信的唯一方法,因为此函数现在正在运行rouge。
$file=$\u获取['file'];
$fileHandle=fopen($file,'w')或die(“”);
fclose($fileHandle);
//现在我们已经有了文件,让我们来做这个过程,或者在本例中只是暂停10秒钟
睡眠(10);
//我们完成了删除文件的任务
取消链接($文件);
}
}