Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从node.js脚本解析HTTP请求_Php_Json_Node.js - Fatal编程技术网

Php 从node.js脚本解析HTTP请求

Php 从node.js脚本解析HTTP请求,php,json,node.js,Php,Json,Node.js,我正在尝试编写一个脚本,该脚本将访问localhost上的脚本,并在node.js脚本中对其进行解析。以下是我目前得到的信息: var http = require('http'); var options = { host:'127.0.0.1', port:'80', path:'/index.php' } var a = http.get(options, function(res){ res.on('data', function(chunk){

我正在尝试编写一个脚本,该脚本将访问
localhost
上的脚本,并在node.js脚本中对其进行解析。以下是我目前得到的信息:

var http = require('http');

var options = {
    host:'127.0.0.1',
    port:'80',
    path:'/index.php'
}
var a = http.get(options, function(res){
    res.on('data', function(chunk){
        console.log(chunk);
    });
    //console.log(res);
});
但这就是它的全部回报:


我知道它是某种类型的流,但我不知道如何处理它。

在向响应对象添加侦听器之前,需要将响应对象的编码设置为“ascii”、“utf8”或“base64”之一。例如:

var a = http.get(options, function(res) {
    res.setEncoding('utf8');
    res.on('data', function(chunk) { // no longer emits a Buffer object
        console.log(chunk);
    });
});

在向响应对象添加侦听器之前,需要将其编码设置为“ascii”、“utf8”或“base64”之一。例如:

var a = http.get(options, function(res) {
    res.setEncoding('utf8');
    res.on('data', function(chunk) { // no longer emits a Buffer object
        console.log(chunk);
    });
});

或者只需使用
chunk.toString()
chunk.toString('utf8')
@AndreySidorov执行可以中断多字节字符的操作。或者只需使用
chunk.toString()
chunk.toString('utf8')
@AndreySidorov执行可以中断多字节字符的操作。