使用proc_open()打开一个指向进程的管道

jackxiang 2008-11-24 14:01 | |
       手机视频广告,用PHP发送视频广告,需要通过PHP执行一个外部发送视频命令。

       可以写PHP扩展来完成,本人没有那个能力及时间,故使用exec()这个函数来完成。

       passthru(),exec(),system() 这三个函数功能相似.passthru无法接受输出值,system只接收最后以行输出,exec可以完全接收输出.
       见如下passthru的一个例子:

文件名称:../passthru.php


   1. <?php
   2. ob_start();
   3. ignore_user_abort();    // run script in background
   4. set_time_limit(0);        // run script forever
   5.
   6. echo '单独dddd';
   7. ob_end_clean();
   8.
   9. passthru('python E:/webroot/test/dtree.py', $return_var);
  10. $output = ob_get_contents();
  11. ob_end_clean();
  12.
  13. echo $output;
  14.
  15. //数据保存
  16. file_put_contents('output.txt', $output);
  17. file_put_contents('return.txt', $return_var);
  18.
  19. die();
  20. ?>


使用如上的方法,当发送若干视频广告时候,需要使用exec调用若干次发送程序,或者让发送程序一次接收若干手机号码,有发送程序循环处理.
       较好的办法是打开一个指向进程的管道,然后每次发送一个广告,如下:

文件名称:../popen.php


   1. <?php
   2. ignore_user_abort();    // run script in background
   3. set_time_limit(0);        // run script forever
   4.
   5. //popen是单向的,如果需要双向支持,使用 proc_open()。
   6. $handle = popen('python E:/webroot/test/dtree.py', 'r');
   7.
   8. while(!feof($handle))
   9. {
  10.     $buffer .= fread($handle, 1024);
  11. }
  12.
  13. pclose($handle);
  14.
  15. echo $buffer;
  16.
  17. //数据保存
  18. file_put_contents('output.txt', $buffer);
  19. die();
  20.
  21. //写入的例子
  22. //$handle = popen ("python","w");
  23. //fwrite($handle, '3 + 6');
  24. //pclose($handle);
  25. ?>




  popen() 它是单向的(只能用于读或写),我们需要获得发送结果,proc_open()来完成双向支持功能:

文件名称:../proc_open.php


   1. <?php
   2. ignore_user_abort();    // run script in background
   3. set_time_limit(0);        // run script forever
   4.
   5. $descriptorspec = array(
   6.                            0 => array('pipe', 'r'),
   7.                            1 => array('pipe', 'w'),
   8.                            2 => array('file', 'c:/temp/error-output4.txt', 'w')
   9.                              );
  10.
  11. $cwd = 'D:/Python24/';
  12. $env = array('some_option' => 'aeiou');
  13.
  14. $process = proc_open('python.exe', $descriptorspec, $pipes, $cwd, $env);
  15.
  16. if (is_resource($process))
  17. {
  18.     // $pipes now looks like this:
  19.          // 0 => writeable handle connected to child stdin
  20.          // 1 => readable handle connected to child stdout
  21.          // Any error output will be appended to /tmp/error-output.txt
  22.          //echo stream_get_contents($pipes[1]);
  23.
  24.     fwrite($pipes[0], "print 61+10\n");
  25.     fwrite($pipes[0], "print 31+10\n");
  26.     fwrite($pipes[0], "print 41+10\n");
  27.     fwrite($pipes[0], "print 61+10\n");
  28.     fclose($pipes[0]);
  29.
  30.     //echo stream_get_contents($pipes[1]);
  31.     $buffer = '';
  32.          while(!feof($pipes[1]))
  33.          {
  34.         $buffer .= fgets($pipes[1], 1024);
  35.          }
  36.
  37.     fclose($pipes[1]);
  38.          echo $buffer;
  39.
  40.     //数据保存
  41.     file_put_contents('output.txt', $buffer);
  42.
  43.     //返回值为1,则发生语法错误!
  44.     $return_value = proc_close($process);
  45.
  46.    
  47.          echo "command returned $return_value\n";
  48. }
  49. ?>






作者:jackxiang@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:http://jackxiang.com/post/1418/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!

评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]