从PHP发布到Node Js

问题描述 投票:0回答:1

我正在尝试在PHP laravel和Node js之间进行通信。

  $ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'http://mylocalip/publish');

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_PORT, 8001);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_POST, true);
$pf = array('clientId' => '123123', 'command' => '{"cmd":"start"}');
curl_setopt($ch, CURLOPT_POSTFIELDS, $pf);
curl_exec($ch);
curl_close($ch);
    dd("Done");

这是m在php中用于发送数据的方式,因为它正在发送数据,但在节点m上未获得任何响应,尝试以角度的方式它按预期方式响应,但在php端它没有响应。

var express = require("express");
var app = express();
    app.post("/publish", function(req, res) {

    console.log(req, res);
    console.log(req.body);
    });
});

我想在控制台中获取从php发送的数据。谢谢,

php node.js laravel express curl
1个回答
0
投票

您必须使用request NPM从第三方api.so中获取数据,因此您的节点应如下所示:

var request = require('request');
request('your URL', function (error, response, body) {
  console.log('error:', error); // Print the error if one occurred
  console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
  console.log('body:', body); // Print the response what your PHP returns.
});
© www.soinside.com 2019 - 2024. All rights reserved.