然后JSON.stringify作为命令行参数发送

时间:2015-09-14 14:25:18

标签: javascript php json node.js child-process

我正在尝试创建一个NodeJS服务器,它使用child_process对一个对象进行字符串化,并将其发送到php文件进行处理,然后等待结果。我的问题是我无法弄清楚如何将我的数据发送到php脚本进行处理,因为当它到达脚本时,数据不再是一个json_decode-able字符串。这是我的NodeJs代码:

dataString=JSON.stringify(data.user);
child_process.exec('php '+global.defaultPath+'php_functions\\authenticate.php ' + dataString, function (err, stdout, stderr){
    if (err) {
        console.log(err);
    }
    stdout=JSON.parse(stdout);
    if (stdout.status==1) global.connections[connection.suid].uid=stdout.uid;
});

这是authenticate.php文件的开头:

if (empty($argv[1])) {
echo "No user to authenticate...";
exit;
}

$user=json_decode($argv[1]);

php文件中的json_decode函数始终返回null,错误代码为4(BAD SYNTAX)。如果我在服务器脚本中记录dataString,它将返回:

{"uid":"1","accounthash":"4971b021ad2824fd42848e06bdce4d2b","suid":"xgv7-1"}

但是当它到达php脚本时,“标记丢失了:

{uid:1,accounthash:4971b021ad2824fd42848e06bdce4d2b,suid:xgv7-1}

我怎样才能将javascript对象发送到PHP脚本?

1 个答案:

答案 0 :(得分:0)

  1. 尝试逃避报价。
  2. 在“{}”
  3. 中将JSON对象作为字符串发送

    EX:

    php -f authenticate.php "{\"uid\":\"1\",\"accounthash\":\"4971b021ad2824fd42848e06bdce4d2b\",\"suid\":\"xgv7-1\"}"