PHP客户端到节点js服务器。连接从不"结束"

时间:2017-12-04 15:02:35

标签: javascript php node.js curl tcp

我有一个简单的网页应该提交" csv"像字符串到服务器。为此,我需要一个API来接收数据字符串并将字符串保存为文本文件。

页面使用PHP,服务器是Node js

代码示例:

PHP:

$ch = curl_init();
$path = '/test.txt';
$url = "http://none.of.your.business.com:8280/";

$data = array('filePath' => $path,
      'message' => $csv);
$data_string = json_encode($string);

curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string))
);

$result = curl_exec($ch);
echo $result;
curl_close($ch);

节点服务器:

var net = require('net');

var HOST = '0.0.0.0';
var PORT = 8280;


net.createServer(function(sock) {

                var body = "";

                console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort);

                sock.on('data', function (data) {
                                body += data;
                                console.log("Partial body: " + body);
                                });

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

                                console.log("ENDED");
                                console.log("Body: " + body);
                                });

}).listen(PORT, HOST);

console.log('Server listening on ' + HOST +':'+ PORT);

使用此代码(以及其他替代方案)" end"永远不会调用event,连接永远不会关闭。导致PHP脚本超时300秒。我认为它的内容相当简单,我应该发回一些东西给PHP来说"完成"?但是我对TCP连接缺乏经验。我得到以下内容" console.log' ged"在服务器上:

[root@xxxxxxxxx fileWriter]# node index.js
Server listening on 0.0.0.0:8280
CONNECTED: xxx.xxx.xxx.xxx:52766
Partial body: POST / HTTP/1.1
Host: xxx.xxx.xxx.com:8280
Accept: */*
Content-Type: application/json
Content-Length: 440

{"filePath":"/test.txt","message":"\"WORKFLOWID\",\"PARENTWORKFLOWID\",\"SOURCEINSTANCEID\",\"WORKFLOWTYPECODE\",\"DATECREATED\",\"DATEUPDATED\",\"ASSETID\",\"PVN\",\"IVN\",\"PROVIDERID\",\"TITLE\",\"SERIESTITLE\",\"OFFERDATE\",\"LASTOFFERDATE\",\"REGION\",\"RELEASEPHASE\",\"WORKFLOWSTATUSID\",\"DURATIONMILLIS\"\\n\"\",\"\",\"\",\"\",\"\",\"04-Dec-17 14.55.05.000000 PM\",\"TITL0\",\"0\",\"0\",\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"-1\",\"\"\n"}

1 个答案:

答案 0 :(得分:0)

服务器端的代码(Node.js)解决了我的问题。似乎发送响应已关闭连接。

const http = require('http');
const fs = require('fs');
http.createServer((request, response) => {
                const { headers, method, url } = request;
                let body = [];
                request.on('error', (err) => {
                                console.error(err);
                                }).on('data', (chunk) => {
                                        body.push(chunk);
                                        }).on('end', () => {
                                                body = Buffer.concat(body).toString();

                                                var json = JSON.parse(body);
                                                response.on('error', (err) => {
                                                                console.error(err);
                                                                });

                                                response.statusCode = 200;
                                                response.setHeader('Content-Type', 'application/json');

                                                const responseBody = { headers, method, url, body };
                                                fs.appendFileSync(json.filePath,json.message);
                                                response.write("Succesfully written " + json.filePath);
                                        //      response.write(JSON.stringify(responseBody));
                                                response.end();

                                });
}).listen(8280);