从php启动phantomjs服务器并等待它的响应 [英] Starting phantomjs server from php and waiting for it's response

查看:573
本文介绍了从php启动phantomjs服务器并等待它的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的php脚本运行一个phantomjs服务器,然后做一个curl请求并读取它的响应(在最终的版本将给出一个生成的pdf的路径)。当从控制台运行phantomjs服务器文件,然后导航到其在浏览器中的地址,一切正常。这是server.js文件:

I wanted to run a phantomjs server from my php script, then do a curl request to it and read it's response (which in the final version will give a path to generated pdf). When running phantomjs server file from the console and then navigating to it's address in the browser, everything works fine. That's the server.js file :

var server, service, page = require('webpage').create(), address, output,
    html = '<!DOCTYPE><html><head></head><body><h1>FOOO</h1></body></html>';

server = require('webserver').create();

var rasterize = function(html, callback){
    address = 'http://localhost';
    output = '/Users/me/print.pdf'
    page.viewportSize = { width: 600, height: 600 };
    page.open(address, function (status) {
        if (status !== 'success') {
            console.log('Unable to load the address!');
        } else {
            window.setTimeout(function () {
                page.content = html;
                page.render(output);
                callback();
            }, 2000);
        }
    });
}

service = server.listen(8080, function (request, response) {
    response.statusCode = 200;

    rasterize(html, function(){
        response.write('<h1>BAR</h1>');
        response.close();
        phantom.exit();     
    });
});

基本上,我打开localhost地址,将页面的内容切换到我的html字符串,然后保存呈现页面为pdf。

Basically I'm opening localhost addres, switching content of the page to my html string and then saving rendered page as pdf.

现在有我的php脚本:

Now comes my php script :

<?
    function send_callback($data){
        echo '{type: success, data: '.$data.'}';
    }

    function curl_post($url, array $post = NULL, array $options = array()) { 
        $defaults = array( 
            CURLOPT_POST => 1, 
            CURLOPT_HEADER => 0, 
            CURLOPT_URL => $url, 
            CURLOPT_FRESH_CONNECT => 1, 
            CURLOPT_RETURNTRANSFER => 1, 
            CURLOPT_FORBID_REUSE => 1, 
            CURLOPT_TIMEOUT => 5, 
            CURLOPT_POSTFIELDS => http_build_query($post) 
        ); 

        $ch = curl_init(); 
        curl_setopt_array($ch, ($options + $defaults)); 
        if( ! $result = curl_exec($ch)) { 
            trigger_error(curl_error($ch)); 
        } 
        curl_close($ch);
        send_callback($result);
    }

        shell_exec('phantomjs '.escapeshellarg(dirname(__FILE__).'/server.js'));
        //wait to allow server to start
        sleep(5);

        $data = array('html'=> 'foo');    
        curl_post('http://localhost:8080', $data);
?>

我在终端中执行 phantomjs server.js 命令,5秒后(到服务器初始化的时间)向它发出curlPOST请求。

现在我两种情况:

Also no magic here. I execute phantomjs server.js command in terminal, and after 5s (time to init the server) do a curlPOST request to it.
Now I have two cases :


  • 如果我从控制台运行php脚本,使用 php script.php 服务器启动,因为我可以看到进程正在运行,图标是可见的在Dock,但我从来没有得到任何响应,并且不创建pdf。

  • 如果我从浏览器运行脚本,图标在dock中不可见,所以服务器以其他方式启动。仍然没有响应或pdf。

  • If I run php script from console, with php script.php the server starts as I can see the process running and the icon is visible in the dock, but I never get any response from it and the pdf is not created.
  • If I run the script from the browser, icon is not visible in the dock so the server starts in some other way. Still no response nor pdf.

任何人都可以看到我的代码有什么错误或想到任何调试方式?

Can anyone see anything wrong in my code or think of any ways of debugging it ?

在OSX 10.7,php 5.3.6,phantomjs latest上测试。 <_ p>

Testing on OSX 10.7, php 5.3.6, phantomjs latest. User _www running the server has admin privileges, folder where I'm writing files were chmoded to 777.

推荐答案

您有几个问题:


  1. phantomjs在前台启动,这意味着您的php脚本停止/睡眠,直到phantomjs停止。请启动phantomjs服务,或在后台运行它。请参阅 php执行后台进程for HowTo。

  2. Web服务器可能无法访问操作系统的图形部分,这意味着它无法与您的桌面交互。这是为什么图标不显示,但phantomjs应该仍然开始。

  1. phantomjs is started in the foreground, which means your php script stops/sleeps until phantomjs stops. Either start the phantomjs service, or run it in the background. See " php execute a background process " for an HowTo.
  2. The web server may probably not access the "graphical part" of your operating system, which means it cannot interact with your desktop. This is why the icon does not appear, but phantomjs should still start.

这篇关于从php启动phantomjs服务器并等待它的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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