我们的代码与此类似:
<?php
ob_implicit_flush(true);
ob_end_flush();
foreach ($arrayOfStrings as $string) {
echo time_expensive_function($string);
}
?>
Run Code Online (Sandbox Code Playgroud)
在Apache中,这会在输出时将每个echo发送到浏览器.然而,在nginx/FastCGI中,由于nginx的工作方式(默认情况下),这不起作用.
有可能使这个工作在nginx/FastCGI上,如果是这样,怎么样?
首先,如果这属于Ask Ubuntu,我很抱歉......我不确定这个论坛是什么.
我正在尝试使用PHP和shell脚本从Github派生并构建一个Android项目.基本上,我是从我写入网站的shell脚本中打印出一些输出.当您访问该页面并按下某些按钮时,该项目将使用Gradle进行分叉和构建.PHP代码运行shell脚本,然后在命令运行时将输出打印到浏览器中.
但是,我收到此错误作为我的脚本的输出:
FAILURE: Build failed with an exception.
* What went wrong:
Failed to load native library 'libnative-platform.so' for Linux amd64.
Run Code Online (Sandbox Code Playgroud)
该脚本现在只是运行gradle build但我打算稍后更改...现在,我正在构建根项目并输出结果.
如果我使用--stacktrace标志运行命令,这是输出:
* Exception is:
net.rubygrapefruit.platform.NativeException: Failed to load native library 'libnative-platform.so' for Linux amd64.
at net.rubygrapefruit.platform.internal.NativeLibraryLoader.load(NativeLibraryLoader.java:49)
at net.rubygrapefruit.platform.Native.init(Native.java:55)
at org.gradle.internal.nativeintegration.services.NativeServices.initialize(NativeServices.java:74)
at org.gradle.internal.nativeintegration.services.NativeServices.initialize(NativeServices.java:60)
at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:203)
at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:169)
at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:33)
at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:22)
at org.gradle.launcher.Main.doAction(Main.java:33)
at org.gradle.launcher.bootstrap.EntryPoint.run(EntryPoint.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:622)
at org.gradle.launcher.bootstrap.ProcessBootstrap.runNoExit(ProcessBootstrap.java:54)
at org.gradle.launcher.bootstrap.ProcessBootstrap.run(ProcessBootstrap.java:35)
at org.gradle.launcher.GradleMain.main(GradleMain.java:23)
Caused by: …Run Code Online (Sandbox Code Playgroud)
我尝试使用shell_exec以与在终端中显示的方式(同时)相似的方式在网页上输出简单的ping命令; 但它只在完成执行后显示,而我需要它显示在终端上显示,
我的代码是
<?php
$i= shell_exec("ping -c 4 google.com");
echo "<pre> $i <pre>";
?>
Run Code Online (Sandbox Code Playgroud)
它正在等待一段时间并将整个东西倾倒在一个镜头上...... PHP可以识别每行的输出并将其显示在网页上
编辑
我也试过这个
<?php
$proc = popen("ping -c 4 google.com", 'r');
echo '<pre>';
while (!feof($proc)) {
echo fread($proc, 4096);
}
echo '</pre>';
?>
Run Code Online (Sandbox Code Playgroud)
但我仍然得到相同的结果..
编辑
当我试图在终端中执行这个PHP代码时,(php test.php)它正常工作,就像我们直接在服务器上执行ping一样.但在网页上它仍然是相同的.
我正在尝试从 php 执行 bash 脚本并实时获取其输出。
我正在应用在这里找到的答案:
然而他们不为我工作。
当我以这种方式调用 .sh 脚本时,它工作正常:
<?php
$output = shell_exec("./test.sh");
echo "<pre>$output</pre>";
?>
Run Code Online (Sandbox Code Playgroud)
然而,当做:
<?php
echo '<pre>';
passthru(./test.sh);
echo '</pre>';
?>
Run Code Online (Sandbox Code Playgroud)
或者:
<?php
while (@ ob_end_flush()); // end all output buffers if any
$proc = popen(./test.sh, 'r');
echo '<pre>';
while (!feof($proc))
{
echo fread($proc, 4096);
@ flush();
}
echo '</pre>';
?>
Run Code Online (Sandbox Code Playgroud)
我的浏览器中没有输出。
我还尝试在这两种情况下调用变量而不是脚本,我的意思是:
<?php
$output = shell_exec("./test.sh");
echo '<pre>';
passthru($output);
echo '</pre>';
?>
Run Code Online (Sandbox Code Playgroud)
这是我的 test.sh 脚本:
#!/bin/bash
whoami
sleep 3
dmesg
Run Code Online (Sandbox Code Playgroud) 我已经看过PHP读取shell_exec实时输出和PHP:在Web浏览器中输出system/Shell_exec命令输出,但无法获得以下工作.
注意:最初,我的shell脚本运行了一些python,但我简化了它.
live.sh
uname -a
date
watch --no-title date
Run Code Online (Sandbox Code Playgroud)
live.php
<?php
$cmd = "sh live.sh";
while (@ ob_end_flush()); // end all output buffers if any
$proc = popen($cmd, 'r');
echo '<pre>';
while (!feof($proc))
{
echo fread($proc, 4096);
@ flush();
}
echo '</pre>';
?>
Run Code Online (Sandbox Code Playgroud)
该uname和date输出出现在浏览器不错,但watch不输出.
我其实是在尝试不可能的事吗?