doutang7661 2014-05-19 01:45
浏览 33
已采纳

如何在我的php文件中使用javascript代码从cronjobs运行url

I have url and i want to execute this url from cronjobs. in my php file there is javascript code to export highchart.

this is my code:

<script>
$(function () {
            var options = {

                title: {
                    text: 'Daily Chart',
                    x: -20 //center
                },
                exporting: {
                    url: 'http://export.highcharts.com/'
                },
                xAxis: {
                    categories: [
                    'A','B',
                    ]
                },
                yAxis: {
                    title: {
                        text: 'IDR'
                    },
                },
                tooltip: {
                    valueSuffix: '°C'
                },
                series: [{
                    showInLegend: false,
                    name: 'Daily',
                    data: [
                        1,2
                    ]
                }]
            };
            var obj = {},
            exportUrl = options.exporting.url;
            obj.options = JSON.stringify(options);
            obj.type = 'image/png';
            obj.async = true;

            var link=$.ajax({
                type: 'post',
                url: exportUrl,
                data: obj,
                dataType: 'html',
                context: document.body,
                global: false,
                async:false,

                success: function (data) {
                    var result=exportUrl+data;
                    return result;
                }
            }).responseText;
            $.ajax({
                        type: 'get',
                        url: 'http://webserver2/index.php/report/render?link='+link+'&id=1',
                        data: link,
                        success: function (data2) {
                            //alert(data2); 
                        }
                    });
});
</script>

If i execute it in my browser i will get file highcart.png to my server. But if i execute in cronjobs i get nothing. Any body know how to execute it with cronjobs??

  • 写回答

1条回答 默认 最新

  • duanpu4143 2014-05-19 02:00
    关注

    JavaScript is a client-side language that runs from within a browser. You can however use something like PhantomJS to run js from the server side. Look at: http://phantomjs.org/

    Edit:

    Download the necessary package from here. Note that these are pre-compiled packages, we will assume you are using Linux.

    You simply save the 'phantomjs' file in your filesystem somewhere (that you can access with crontab).

    Save your javascript as a .js file extension.

    Add a cron job like:

    30 08 10 06 * /path/to/phantomjs javscriptfile.js

    This will run 30 of June at 8.30am (You will need to schedule it when you want.)

    Hopefully this should work as intended, although it does seem you are using jQuery functions so you might need to include the jQuery source code in your .js file.

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

报告相同问题?

悬赏问题

  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿