2017-05-01 80 views
1

我想從我的nodejs文件中調用php文件。我已經安裝並要求exec-php模塊,它已經安裝好了。我得到的錯誤是:NodeJs exec-php模塊,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()通過:

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

我在這段代碼中做了什麼錯誤,會影響錯誤嗎?另外,如果有更高效的方式來做這件事,請讓我知道,因爲我花了幾個小時已經試圖弄清楚如何讓這些文件順利運行。

回答

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?

+0

我在哪裏可以找到PHP bin文件夾?我從來沒有見過它。 –

+0

查看我更新的anser! –

+0

我想我不得不將它安裝到我的電腦上,因爲php.exe不會返回任何內容。當我安裝它時我會回覆你 –