dougaoshang0237 2016-09-29 20:42
浏览 126
已采纳

PHP挂起问题与passthru()/ system()调用和输出缓冲

I do have a strange problem. I'm trying to get around output buffering in PHP to run system() calls and get instant output. It works very well, but for some strange reason, the ping command on "test.de" isn't presenting instant output as the ping on google.de does. I do see the first line for 'ping test.de' in the linux shell instantly, but it doesn't appear instantly with this script. Anyone can help?

<?php

  // Turn off output buffering
  ini_set('output_buffering', 'off');
  // Turn off PHP output compression
  ini_set('zlib.output_compression', false);
  // Implicitly flush the buffer(s)
  ini_set('implicit_flush', true);
  ob_implicit_flush(true);
  // Clear, and turn off output buffering
  while (ob_get_level() > 0) {
      // Get the curent level
      $level = ob_get_level();
      // End the buffering
      ob_end_clean();
      // If the current level has not changed, abort
      if (ob_get_level() == $level) break;
  }
  // Disable apache output buffering/compression
  if (function_exists('apache_setenv')) {
      apache_setenv('no-gzip', '1');
      apache_setenv('dont-vary', '1');
  }

  header('Cache-Control: no-cache');

  $i=0;
  while( $i < 1000 ) {
    $i++;
    echo '                        ';
  }
  echo '<pre>';
  echo "Pinging google.de 

";
  passthru("ping -w 10 -c 4 google.de");
  echo "

Pinging test.de 

";
  passthru("ping -w 10 -c 4 test.de");
  echo '</pre>';
?>

I've marked the delayed line in this output:

Pinging google.de 

PING google.de (172.217.16.131) 56(84) bytes of data.
64 bytes from zrh04s06-in-f131.1e100.net (172.217.16.131): icmp_seq=1 ttl=56 time=22.0 ms
64 bytes from zrh04s06-in-f131.1e100.net (172.217.16.131): icmp_seq=2 ttl=56 time=22.0 ms
64 bytes from zrh04s06-in-f131.1e100.net (172.217.16.131): icmp_seq=3 ttl=56 time=22.2 ms
64 bytes from zrh04s06-in-f3.1e100.net (172.217.16.131): icmp_seq=4 ttl=56 time=22.0 ms

--- google.de ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 22.016/22.067/22.200/0.130 ms


Pinging test.de 

PING test.de (104.45.6.189) 56(84) bytes of data. <<<< THAT line is delayed!!

--- test.de ping statistics ---
11 packets transmitted, 0 received, 100% packet loss, time 9999ms
  • 写回答

2条回答 默认 最新

  • douduiti3040 2016-09-30 08:28
    关注

    Solution:

    Apparently it has something to do with linux buffering. I've reworked the shell command so it is working now. The solution is to wrap the linux command with the script command. So:

    passthru("script -q -c 'ping -w 10 -c 4 test.de' /dev/null")
    

    This article helped: https://unix.stackexchange.com/questions/25372/turn-off-buffering-in-pipe

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集