服务器并等待它的响应

服务器并等待它的响应

本文介绍了从 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();
    });
});

基本上我打开本地主机地址,将页面内容切换到我的 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 最新版本上测试.运行服务器的用户 _www 具有管理员权限,我正在写入文件的文件夹被修改为 777.

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 执行后台进程"了解操作方法.
  2. 网络服务器可能无法访问您操作系统的图形部分",这意味着它无法与您的桌面交互.这就是图标没有出现的原因,但是phantomjs应该还是可以启动的.

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

09-05 09:24