I'm running into a problem where the php script never terminates due to its work, so it never gets a chance to pass data back to ajax to display back to the user in html format.
我遇到了一個問題,PHP腳本由於其工作而永遠不會終止,因此它永遠不會有機會將數據傳回ajax以html格式顯示回用戶。
To be more clear, what I'm trying to do is have a user click on a button on an html page, the button executes a javascript function which uses ajax to execute the php script. The php script executes a never ending command (ie: ping localhost) using popen and then I echo the output from popen. The problem is that because the php script never finishes, the echos are never reported back unless I kill the ping process, which causes the php script to end and the data echoed returned back to the javascript function to display in html.
更清楚的是,我要做的是讓用戶點擊html頁面上的按鈕,該按鈕執行一個javascript函數,該函數使用ajax來執行php腳本。 php腳本使用popen執行一個永不停止的命令(即:ping localhost),然后我回顯popen的輸出。問題是因為php腳本永遠不會完成,所以回不會報告回聲,除非我終止ping進程,這導致php腳本結束並且數據回顯返回到javascript函數以在html中顯示。
I want to be able to see the live output when the php script is running. If I click on that button in the html website, I want the button to call the javascript function, then execute the php script and I want to see immediately every echo from the php script as it is running live. Is that possible?
我希望能夠在php腳本運行時看到實時輸出。如果我點擊html網站上的那個按鈕,我希望按鈕調用javascript函數,然后執行php腳本,我想立即看到php腳本中的所有回顯,因為它正在運行。那可能嗎?
Here is my code so far:
這是我到目前為止的代碼:
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.0.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
function execfunc() {
// Clear any previous output.
clearOutput();
$.ajax({
type: "GET",
url: "phpscript.php",
data: "",
success: function(msg){
$('#divOutput').html(msg);
}
}); // Ajax Call
}
function stopReadout() {
// to do
}
function clearOutput() {
$("#divOutput").html("");
}
</script>
</head>
<body>
<h3>Executing Linux Command</h3>
<p>Push the button to execute the command "ping localhost":</p>
<button type="button" onclick="execfunc();">Execute</button>
<button type="button" onclick="stopReadout();">Stop</button>
<button type="button" onclick="clearOutput();">Clear</button>
<br/>
<p id="label">Terminal output:</p>
<br/>
<div id="divOutput" style="width:800px;height:512px;border:1px solid black;overflow:scroll"></div>
</body>
phpscript.php
<?php
while (@ ob_end_flush()); // end all output buffers if any
$cmd = 'ping localhost';
$proc = popen($cmd, 'r');
echo '<pre>';
while (!feof($proc))
{
echo fread($proc, 4096);
flush();
}
echo '</pre>';
?>
If you run the phpscript.php by itself, it works fine and echos back to the browser window. But if I execute the script from the index.html javascript using ajax, I don't get any live output back because the php script never finishes.
如果您自己運行phpscript.php,它可以正常工作並回顯到瀏覽器窗口。但是如果我使用ajax從index.html javascript執行腳本,我不會得到任何實時輸出,因為php腳本永遠不會完成。
I even tried having phpscript.ph make a fifo pipe and dump the ping output into the fifo pipe, then I had another php script that echoed back 'cat fifofile', but the same behavior happens.
我甚至嘗試讓phpscript.ph制作一個fifo管道並將ping輸出轉儲到fifo管道中,然后我有另一個PHP腳本回顯'cat fifofile',但是同樣的行為發生了。
Any suggestions or help would truly be appreciated it! D
任何建議或幫助將真正受到贊賞! d
0
You need to use web sockets for that.
您需要使用Web套接字。
本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/04/08/305add3039236ae540b6385337715913.html。