简体   繁体   English

获取node.js以在pollingLoop中执行本地.php文件

[英]Get node.js to execute local .php file inside pollingLoop

Short here's the purpose of my application. 简而言之,这就是我申请的目的。

I have a PHP file reading data through an ODBC connection and writing them to a local database. 我有一个PHP文件,它通过ODBC连接读取数据并将其写入本地数据库。

This PHP file needs to be run LOCAL ON THE SERVER each time a loop is processed in my node.js, socket.io server. 每次在我的node.js,socket.io服务器中处理循环时,都需要在服务器上本地运行此PHP文件。

My setup is Apache, PHP 5.5.12 and node.js. 我的设置是Apache,PHP 5.5.12和node.js。

I was pretty convinced there was a simple way to this but I haven't had any luck getting Ajax or similar to work inside node.js by following other guides. 我非常相信有一个简单的方法可以做到这一点,但是通过遵循其他指南,我对在Ajax或类似的东西在node.js中工作没有任何运气。

The code where the file should be processed inside looks like this. 应该在其中处理文件的代码如下所示。

var pollingLoop = function () {
    // Update the local database
    // HERE I WANT THE PHP FILE TO EXECUTE

    // Make the database query
    var query = connection.query('SOME LONG SQL QUERY IN HERE'),
        status = []; // this array will contain the result of our db query

    // set up the query listeners
    query
        .on('error', function(err) {
            // Handle error, and 'end' event will be emitted after this as well
            console.log( err );
            updateSockets( err );
        })
        .on('result', function( runningstatus ) {
            // it fills our array looping on each runningstatus row inside the db
            status.push( runningstatus );
        })
        .on('end',function(){
            // loop on itself only if there are sockets still connected
            if(connectionsArray.length) {
                pollingTimer = setTimeout(pollingLoop, POLLING_INTERVAL);
                updateSockets({status:status});
            }
        });
};

Am I totally of track by trying to do that? 我是否完全可以做到这一点?

Try invoking php through the shell interface: 尝试通过shell界面调用php:

var exec = require("child_process").exec;
app.get('/', function(req, res){exec("php index.php", function (error, stdout, stderr) {res.send(stdout);});});

OR this link 此链接

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

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