简体   繁体   English

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

[英]How to run url from cronjobs with javascript code in my php file

I have url and i want to execute this url from cronjobs. 我有网址,我想从cronjobs执行此网址。 in my php file there is javascript code to export highchart. 在我的php文件中,有JavaScript代码可以导出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. 如果我在浏览器中执行它,我将把文件highcart.png发送到我的服务器。 But if i execute in cronjobs i get nothing. 但是,如果我在cronjobs中执行,我什么也得不到。 Any body know how to execute it with cronjobs?? 任何人都知道如何使用cronjobs执行它吗?

JavaScript is a client-side language that runs from within a browser. JavaScript是一种在浏览器中运行的客户端语言。 You can however use something like PhantomJS to run js from the server side. 但是,您可以使用PhantomJS之类的东西从服务器端运行js。 Look at: http://phantomjs.org/ 查看: http//phantomjs.org/

Edit: 编辑:

Download the necessary package from here . 此处下载必要的软件包。 Note that these are pre-compiled packages, we will assume you are using Linux. 请注意,这些是预编译的软件包,我们假设您使用的是Linux。

You simply save the 'phantomjs' file in your filesystem somewhere (that you can access with crontab). 您只需将“ phantomjs”文件保存在文件系统中的某个位置(可以使用crontab进行访问)。

Save your javascript as a .js file extension. 将您的JavaScript保存为.js文件扩展名。

Add a cron job like: 添加一个cron作业,例如:

30 08 10 06 * /path/to/phantomjs javscriptfile.js 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.) 该活动将于6月30日上午8.30运行(您需要根据需要安排时间。)

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. 希望这应该能够按预期工作,尽管您似乎确实在使用jQuery函数,所以您可能需要在.js文件中包含jQuery源代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM