0

我是新来的nodejs

我想创建应用程序使用nodejsphp

这样的方案..

nodejs-->id--> php(向mysql插入数据)

nodejs将值 id 发送到php页面(php从中接收数据nodejs) 。php我想将数据插入到mysql

我的问题是我如何向phpfrom发送值nodejs以便php接收数据?我是如何做到这一点的?任何教程解释这个?

谢谢

4

2 回答 2

0

所以我猜你本质上是想在一个运行 PHP 的 node.js 服务器上移动数据?在这种情况下,您需要使用从 node.js 到运行 PHP 实例的 URL 的 Web 请求。我会推荐 Mikeal 的优秀请求包。

你可以像这样使用它:

var request = require('request');

//so we assume this function is called with some
//string value for id, and some string representing
//the URL for your PHP endpoint, e.g.:
//>sendIdToURL('foo', 'http://mydomain.com/myendpoint.php')
function sendIdToURL(id, url) {
    request(url, {
        method: 'POST',
        body: id
    }, function(err, response) {
        if(err) throw err;
        else {
            //do something with the response if you want
        }
    });
} 

然后在 PHP 结束时,您的$HTTP_RAW_POST_DATA, orfile_get_contents("php://input")将具有值 'foo'。

这能回答问题吗?这是一个有趣的用例,您必须使用 php 进行 mysql 连接......

于 2013-05-06T02:39:48.770 回答
0

节点端发布到您的 php 文件。喜欢:

var postRequest = {
    host: "mydomain.com",
    path: "/myfile.php?do=insert&field1=somedata",
    port: 80,
    method: "POST",
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'  
    }
};

和 php 方面接收这些东西,如:

$_POST['...']

然后执行 db 操作。

继承人更多信息:node.js POST 请求失败

希望能帮助到你

于 2013-05-06T02:41:34.630 回答