php通过js发送请求,用PHP cURL向Node.js发送Post请求

我试图通过PHP cURL向我的node.js服务器发送请求,然后向客户端发送消息。服务器正在运行和设置如下:

var app = require('http').createServer(handler)

, io = require('socket.io').listen(app)

, fs = require('fs')

, qs = require('querystring')

app.listen(8000);

function handler(req, res) {

// set up some routes

switch(req.url) {

case '/push':

if (req.method == 'POST') {

console.log("[200] " + req.method + " to " + req.url);

var fullBody = '';

req.on('data', function(chunk) {

fullBody += chunk.toString();

if (fullBody.length > 1e6) {

// FLOOD ATTACK OR FAULTY CLIENT, NUKE REQUEST

req.connection.destroy();

}

});

req.on('end', function() {

// Send the notification!

var json = qs.stringify(fullBody);

console.log(json.message);

io.sockets.emit('push', { message: json.message });

// empty 200 OK response for now

res.writeHead(200, "OK", {'Content-Type': 'text/html'});

res.end();

});

}

break;

default:

// Null

};

}我的PHP如下所示:

$curl = curl_init();

$data = array('message' => 'simple message!');

curl_setopt($curl, CURLOPT_URL, "http://localhost:8000/push");

curl_setopt($curl, CURLOPT_POST, 1);

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

curl_exec($curl);控制台说json.message是未定义的。为什么没有定义?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值