繁体   English   中英

将请求从PHP脚本发送到Node.js脚本

[英]Send request from PHP script to the Node.js script

有没有一种方法可以将任何形式的请求从PHP脚本发送到Node.js脚本?

例如,我有以下目录:

scripts
|_sender.php
|_receiver.js

我想从php脚本发送一些数据,并使用node.js脚本读取它以执行一些操作。

如何正确完成?

这取决于js在哪里读取传入的数据

如果是服务器,请从node receiver.js启动它,然后将其从php发送到http:// local host /。

或者,您也可以将php输出转储到文件中,然后由接收方读取

您应该提供更多信息以获得更好的答案

我使用的最简单的方法是使用HTTP post或get将PHP数据传递到节点,这是我的代码,用于将数据从PHP发送到节点。

// Node Side
express = require('express');
bodyParser = require('body-parser');
express.use(bodyParser.json());

express.post('/get_php_data', function (req, res) {
    // php array will be here in this variable
    var data = req.body.data;


    res.send(' Done ');
});

httpPost('NODE_URL:2200/get_php_data', array('data' => 'some data'));

// PHP Side
function httpPost($url,$params)
{
    $postData = http_build_query($params);
    $ch = curl_init();  

    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_HEADER, false); 
    curl_setopt($ch, CURLOPT_POST, count($postData));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);    

    $output=curl_exec($ch);

    curl_close($ch);
    return $output;
} 

暂无
暂无

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

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