从PHP调用node.js的脚本返回什么 [英] Calling node.js script from PHP returns nothing

查看:1182
本文介绍了从PHP调用node.js的脚本返回什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的就是在理论上很简单,但我不能完全得到它的工作。

What I want to do is simple in theory, but I cannot quite get it to work.

我写了使用要求包异步获取一些数据,分析它,并吐出来为HTML一个简单的node.js脚本。我想整合我的客户的PHP和基于Apache的网站,这是一个共享主机上的这个脚本,并遇到了一些碰壁:

I wrote a simple node.js script that uses the request package to asynchronously fetch some data, parse it, and spit it out as html. I wanted to integrate this script in my client's php and apache based website which is on a shared host, and ran into some snags:


  1. 没有mod_proxy的,所以我不能简单地运行我的节点脚本作为服务器和代理通过Apache

  2. 我不希望运行在80端口和代理节点从节点到Apache。这只是太多矫枉过正什么,我需要做的,并会引入太多的麻烦我。我特别共享主机是已知有麻烦保持节点服务器实例了,我不能只为这个脚本的运行证明潜在的停机时间。

  3. 我试过节点CGI 包,但它并没有为我工作。我对错误不存在内部节点的方法,我觉得这个包仅仅是过时的。

  1. There is no mod_proxy, so I can't simply run my node script as a server and proxy through Apache
  2. I don't want to run node on port 80 and proxy to apache from node. It's just way too much overkill for what I need to do, and would introduce too many headaches for me. My particular shared host is known to have trouble keeping node server instances up, and I can't justify potential downtime just for this script to run.
  3. I tried the node-cgi package but it didn't work for me. I got errors about internal node methods not existing, I think this package is just out of date.

所以,我已经降落在试图简单地从PHP调用节点。我的整个的index.php 文件是:

So what I have landed on is trying to simply call node from PHP. My whole index.php file is:

<?php
  header("Content-Type: text/html");
  exec("node beerlist.nd", $output);
  echo implode('', $output);

当我执行 PHP的index.php 在命令行中,我得到我预期的输出,但是,当我尝试从浏览器访问这个,我什么也没得到,即的Content-Length:0 。为什么呢?

When I execute php index.php on the command line, I get my expected output, however, when I try to access this from the browser, I get nothing ie Content-Length: 0. Why?

我想也许这与我的节点脚本的异步性的事,但好像这个过程保持活着,直到它完成所有的异步调用。所以,不应该我的PHP代码片段发送输出到浏览器没有任何麻烦?缺少什么我在这里?

I thought maybe it had to do with the async nature of my node script but it seems like the process stays alive until it finishes all the async calls. So shouldn't my php snippet send the output to the browser without any trouble? What am I missing here?

编辑:我节点的脚本这个要点是

This gist of my node script is

var req = require('request')

req("http://mywebsite.com", function(err, resp, body) {
  var output = // pull some interesting pieces out of the whole body
  console.log(output);
});

我的输出信号的产生变量不是中央对这里的问题。相关部分是我使用要求进行异步调用,并使用的console.log 来输出我的结果..也许这是一个问题?

The generation of my output variable is not central to the issue here. The relevant parts are that I use request to make an asynchronous call and use console.log to output my results... maybe this is a problem?

推荐答案

我想Apache用户不知道节点命令是什么。如果我是正确的尝试在php文件写的:

I suppose Apache user doesn't know what node command is. If I'm right try to write in php file:

<full path to node> beerlist.nd

而不是

node beerlist.nd

要获得完整路径终端节点运行的节点

To get full path to node run in terminal which node

这篇关于从PHP调用node.js的脚本返回什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆