doutang7661 2014-05-18 17: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-18 18: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.

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

报告相同问题?