node.js系列(模块):request模块实现与php的通讯

app.js:

var express = require('express');
var request = require('request');

var app = express();

/*request('http://127.0.0.1:8000/test.php', function (error,response,body) {
    if (!error && response.statusCode == 200)
    {
        console.log(body);
    }
});*/

request.post({url:'http://127.0.0.1:8000/test.php', form: {"name" : "john"}}, function (err, httpResponse, body) {
    if (err) {
        return console.error('upload failed:', err);
    }
    console.log('Upload successful!  Server responded with:', body);
});

app.listen(3000,'127.0.0.1', function () {
    console.log('server is started');
});

  

<?php
$arr = array('name'=>'node.js','age'=>'25','message'=>'十里平湖霜满天','new'=>$_POST['name']);
echo json_encode($arr);

  执行结果:

posted @ 2016-08-12 18:17  星际实验室  阅读(1066)  评论(0编辑  收藏  举报