【问题标题】:NodeJs exec-php module, php is not a commandNodeJs exec-php 模块,php 不是命令
【发布时间】:2017-09-29 02:36:10
【问题描述】:

我正在尝试从我的 nodejs 文件中调用 php 文件。我已经安装并需要exec-php 模块,并且安装得很好。我得到的错误是:

{ Error: Command failed: php C:\Users\***\Desktop\nodejs\xxchat11\node_modules\exec-php\lib\php\cli.php -pC:\Users\***\AppData\Local\Temp\tmp-129647qzlx6s.gh55ewmi.tmp -rC:\Users\***\AppData\Local\Temp\tmp-12964
7pid95t.8vpiizfr.tmp
'php' is not recognized as an internal or external command,
operable program or batch file.
   at ChildProcess.exithandler (child_process.js:204:12)
    at emitTwo (events.js:106:13)
    at ChildProcess.emit (events.js:191:7)
    at maybeClose (internal/child_process.js:886:16)
    at Socket.<anonymous> (internal/child_process.js:342:11)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at Pipe._handle.close [as _onclose] (net.js:501:12)
  killed: false,
  code: 1,
  signal: null,
  cmd: 'php C:\\Users\\***\\Desktop\\nodejs\\xxchat11\\node_modules\\exec-php\\lib\\php\\cli.php -pC:\\Users\\***\\AppData\\Local\\Temp\\tmp-129647qzlx6s.gh55ewmi.tmp -rC:\\Users\\***\\AppData\\Local\\Temp\\tmp-
129647pid95t.8vpiizfr.tmp' }

我的php 文件是:

<?php
$hashes = array('md2','md4','md5','sha1','sha224','sha256','sha384','sha512','ripemd128','ripemd160','ripemd256','ripemd320','whirlpool','tiger128,3','tiger160,3','tiger192,3','tiger128,4','tiger160,4','tiger192,4','snefru','snefru256','gost','gost-crypto','adler32','crc32','crc32b','fnv132','fnv1a32','fnv164','fnv1a64','joaat','haval128,3','haval160,3','haval192,3','haval224,3','haval256,3','haval128,4','haval160,4','haval192,4','haval224,4','haval256,4','haval128,5','haval160,5','haval192,5','haval224,5','haval256,5');
function hash($method. $plaintext){
  if(in_array($method, $hashes)){
    $hashed = hash($method, $plaintext);
    echo 'The '.$method.' hash, for the string `'.$plaintext.'` is: '.$hashed;
  } else {
    echo "Method not found! Please type !help to see the list of supported methods.";
  }
}
?>

在我的nodejs 文件中,我通过以下方式调用 php 文件:

var execPhp = require('exec-php');

app.get('/hash.php/:method/:text', function(req,res){
  execPhp(__dirname+'/hash.php', function(error, php, output){
    console.log(error);
    //php.hash(req.params.method, req.params.text, function(err, result){
        //res.send(result);
    //});
  });
});

另外,在我的index 文件中,我正在运行app.get() by:

$.get('hash.php/'+method+'/'+plaintext, function(data) {
    $chat.append('Chatbot: <strong>'+data+'</strong><Br />');
});

我是否在这段代码中做错了会影响错误的事情?另外,如果有更有效的方法,请告诉我,因为我已经花了几个小时试图弄清楚如何让这些文件顺利运行。

【问题讨论】:

    标签: php node.js ajax exec call


    【解决方案1】:

    exec-php 接受 3 个参数:

    1. String。用户 php 文件的路径。
    2. String机器 php bin 文件的路径。
    3. Function。创建 exec-php 对象后的回调函数。

    您缺少第二个参数,即 php 的路径,这就是您收到该错误的原因。

    execPhp(__dirname+'/hash.php', 'PATH_TO_PHP', function //...
    

    获取 php 位置:

    Windows:

    C:\>where php.exe
    

    Linux

    which php
    

    查看这个答案:How to determine path to php.exe on windows - search default paths?

    【讨论】:

    • 在哪里可以找到 PHP bin 文件夹?我没见过。
    • 我想我必须将它安装到我的计算机上,因为 php.exe 没有返回任何内容。安装完成后我会回复你
    • 你没有安装php?
    • 我刚刚下载并移动到C:/但仍然没有,php.net/get/php-7.1.4.tar.bz2/from/a/mirror
    • 啊,我不知道为什么,但是 where php.exe 命令对我不起作用。我在 c:/ 的 xampp 文件夹中找到了它。谢谢你帮助我:)
    猜你喜欢
    • 2013-09-07
    • 2021-08-05
    • 2017-05-06
    • 1970-01-01
    • 1970-01-01
    • 2014-05-17
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多