php - 管道标准输出到浏览器

标签 php c++

我有一个 C++ 程序,它读取文件并一次打印一行(每秒打印一行)。

我需要从 PHP 脚本中执行代码并将输出通过管道传回浏览器。到目前为止,我已经尝试了 execpassthru,但在这两种情况下,程序执行后整个输出都会“转储”到浏览器上。

如何让 PHP 将输出流式传输回浏览器。

这是我到目前为止编写的代码:
sender.php:发送请求执行。

<?php 

/*
 * Purpose of this program:
 * To display a stream of text from another C++-based program.
 * Steps:
 * 1. Button click starts execution of the C++ program.
 * 2. C++ program reads a file line-by-line and prints the output.
 */

?>

<html>

<head>
    <script type="text/javascript" src="js/jquery-1.11.2.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $("#startDisplaying").click(function() {
                console.log("Starting to display.");
                /*
                //initialize event source.
                if(!!window.EventSource) {
                    console.log("Event source is available.");
                    var evtSource = new EventSource("receiver.php");

                    evtSource.addEventListener('message', function(e) {
                        console.log(e.data);
                    }, false);

                    evtSource.addEventListener('open', function(e) {
                        console.log("Connection opened.");
                    }, false);

                    evtSource.addEventListener('error', function(e) {
                        console.log("Error seen.");
                        if(e.readyState == EventSource.CLOSED) {
                            console.log("Connection closed.");
                        }
                    }, false);
                } else {
                    console.error("Event source not available.");
                }
                */
                $.ajax({
                    type: 'POST',
                    url: 'receiver.php',
                    success: function(data) {
                        console.log("Data obtained on success: " + data);
                        $("#displayText").text(data);
                    },
                    error: function(xhr, textStatus, errorThrown) {
                        console.error("Error seen: " + textStatus + " and error = " + errorThrown);
                    }
                });
            });
        });
    </script>
</head>

<body>
<textarea id="displayText"></textarea><br/>
<button id="startDisplaying">Start Displaying</button>
</body>

</html>  

执行程序的receiver.php:

<?php 

/* This file receives the request sent by sender.php and processes it.
 * Steps:
 * 1. Start executing the timedFileReader executable.
 * 2. Print the output to the textDisplay textarea.
 */

header('Content-Type: text/event-stream');
header('Cache-control: no-cache');

function sendMsg($id, $msg) {
    echo "id: $id".PHP_EOL;
    echo "data: $msg".PHP_EOL;
    echo PHP_EOL;
    ob_flush();
    flush();
}

$serverTime = time();

$cmd = "\"timedFileReader.exe\"";
//sendMsg($serverTime, 'server time: '.exec($cmd, time()));
passthru($cmd);

/*
$cmd = "\"timedFileReader.exe\"";
exec($cmd. " 2>&1 ", $output);
print_r($output);
*/
?>

C++ 程序:

#include<fstream>
#include<string>
#include<unistd.h>
#include<cstdlib>
#include<iostream>
using namespace std;

int main(int argc, char *argv[]) {

  ifstream ifile;
  string line;

  ifile.open("file.txt");
  if(!ifile) {
    cout << "Could not open file for reading." << endl;
    exit(0);
  }

  while(getline(ifile, line)) {
    cout << line << endl;
    //usleep(5000000);  //sleep for 1000 microsecs.
    sleep(1);
  }

  return 0;
}

这种执行模型在 PHP 中是否可行?
欢迎任何帮助。

最佳答案

引用http://php.net/manual/en/function.popen.php .

它也可能依赖于 HTTP 服务器,因为它可以在您读取 C++ 应用程序输出之前关闭浏览器 session 。

不确定您使用 PHP 到 C++ 桥接器的原因,但根据您的代码 - 您可以仅在 PHP 中完成整个工作。或者使用 C++ CGI 接口(interface)...

(类似问题:how get the output from process opend by popen in php?)

关于php - 管道标准输出到浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30007522/

相关文章:

c++ - 在 Windows 7 上使用 IFileDialog 的问题

php - $_SESSION 变量存储在哪里?

php - 在不提交 PHP 表单的情况下单击按钮重定向到页面

PHP fatal error : Call to undefined function?

c++ - 如何枚举Qt中所有的QObject类?

c++ - 我们可以在 C++ 中的函数中包含函数吗?

c++ - 如何将 ceres::CubicInterpolator 与不在统一网格上的数据一起使用

c++ - xstring 中的无效空指针

javascript - 如何在页面加载时隐藏另一个 php 文件中的 div

php - 在 PHP 中搜索给定数组的部分查询