duanmo6937 2015-06-11 22:13
浏览 93
已采纳

用于PhantomJS + Highcharts的Javascript中的PHP

I have just configured phantomjs which is working fine. I have it installed in order to create charts from highcharts and include them in automated email through cronjobs, based on user's info.

This will be done in php and right now I have the execution running perfectly. My php is:

exec('/usr/bin/phantomjs myscript.js');

and my test myscript.js is:

var page = require('webpage').create();
page.open('http://github.com/', function() {
page.render('github.png');
phantom.exit();
});

This saves github.png in the same directory so all is good. But I need to do this with highcharts and all the examples I found have static data (with apples, bananas, and all that as filler data). If I need to have this created dynamincally from data stored in a sql table can I use php within the javascript to get the user info?

For example I'd like to do something like below:

    ...title: {
        text: 'Combination chart'
    },
    xAxis: {
        categories: [<? echo $xaxis; ?>]
    },...

How can I accomplish this? Am I able to pass paramaters through the exec function in my original php file? Thanks!

  • 写回答

1条回答 默认 最新

  • dongwei8440 2015-06-12 02:05
    关注

    So I've figured it out. To anyone having the same issue here's the answer:

    in php file calling myscript.js use the following format for passing arguments through exec():

    exec('/usr/bin/phantomjs myscript.js param1 param2',$op,$er);
    

    Then in the js file do the below to get the parameters passed:

    var args = require('system').args;
    var page = require('webpage').create();
    var address = args[1];
    var saveme = args[2];
    page.open(address, function() {
       page.render(saveme);
       phantom.exit();
    });
    

    Voila!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?