只要创建png,就可以获取PHP proc_open()来读取PhantomJS流 [英] Get PHP proc_open() to read a PhantomJS stream for as long as png is created

查看:107
本文介绍了只要创建png,就可以获取PHP proc_open()来读取PhantomJS流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个依赖于shell_exec()的PHP脚本,并且(因此)在99%的时间内工作. 该脚本执行了生成图像文件的PhantomJS脚本. 然后,使用更多的PHP以某种方式处理了该图像文件. 问题是shell_exec()有时会挂起并引起可用性问题. 读这个 https://github.com/ariya/phantomjs/issues/11463 我了解到shell_exec()是问题所在,切换到proc_open可以解决问题.

I had a PHP script that relied on shell_exec() and (as a result) worked 99% of the time. The script executed a PhantomJS script that produced an image file. Then using more PHP that image file was processed in a certain way. Problem was that on occasion shell_exec() would hang and cause usability issues. Reading this https://github.com/ariya/phantomjs/issues/11463 I learnt that shell_exec() is the problem and switching to proc_open would solve the hanging.

问题在于,当shell_exec()等待执行的命令完成时,proc_open却没有,因此跟随它并在生成的图像上工作的PHP命令失败,因为该图像仍在生成中. 我正在Windows操作系统上工作,所以不能选择pcntl_waitpid.

The problem is that while shell_exec() waits for the executed command to finish proc_open doesn't, and so the PHP commands that follow it and work on the generated image fail as the image is still being produced. I'm working on Windows so pcntl_waitpid is not an option.

我想做的是持续让PhantomJS输出任何内容,以便proc_open通过其stdin管道进行读取,这样我就可以安排图像处理PHP函数的时间,使其在目标图像尽快开始工作文件已准备好.

What I'm trying to do is continuously have PhantomJS output something (anything) so that proc_open would read via it's stdin pipe and that way I can time the image processing PHP functions to start working as soon as the target image file is ready.

这是我的phantomJS脚本:

here is my phantomJS script:

interval = setInterval(function() {
  console.log("x");
}, 250);
var page = require('webpage').create();
var args = require('system').args;
page.open('http://www.cnn.com', function () {
  page.render('test.png');
  phantom.exit();
});

还有我的PHP代码:

ob_implicit_flush(true);
$descriptorspec = array(
    0 => array("pipe", "r"),  // stdin
    1 => array("pipe", "w"),  // stdout
    2 => array("pipe", "w")   // stderr 
);

$process = proc_open ("c:\phantomjs\phantomjs.exe /test.js", $descriptorspec, $pipes);
if (is_resource($process))
{
while( ! feof($pipes[1]))
  {
     $return_message = fgets($pipes[1], 1024);
     if (strlen($return_message) == 0) break;
     echo $return_message.'<br />';
     ob_flush();
     flush();
  }
}

生成了test.png,但是我没有得到一个$return_message.我在做什么错了?

The test.png is generated, but I am not getting a single $return_message. What am I doing wrong?

推荐答案

比尔·尚德(Bill Shander)在您链接的github问题中建议了,您可以使用:

Proc_Close(Proc_Open("phantomjs test.js &", Array (), $foo));

运行您的phantomjs脚本(基于此答案).似乎您只需要图像,因此在这种情况下不需要管道.

to run your phantomjs script (which is based on this answer). It seems that you only need the image, so the pipes are not necessary in this case.

完整的参考脚本是此处,并且可以在Windows上直接使用.

Complete script for reference is here and works on windows as is.

这篇关于只要创建png,就可以获取PHP proc_open()来读取PhantomJS流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆