2017-03-17 64 views
-2

我在使用exec()循環中使用php的nodejs腳本時遇到了問題。 問題是,腳本只執行一次,然後只是錯誤。 這裏是PHP代碼:來自php的exec nodejs腳本

for ($i=0; $i <= ((mysqli_num_rows($repp)/50)-1); $i++) 
{ 

    $req = "select * from items limit ".$id.",50"; 
    $rep = mysqli_query($link,$req); 
    $names = ""; 
    $id = $id + 50; 

    while($donne = mysqli_fetch_array($rep)) 
    { 
      $names = $names.$donne["item_name"].','; 
    } 
    $resultat = '"'.rtrim($names, ",").'"'; 
    exec("node node_opskins.js ".$resultat,$out); 
    $result =implode($out); 
    $a = str_replace("'", '"', $result); 
    $a = preg_replace('/([\w\d]+): /', '"$1": ', $a); 
    foreach (json_decode($a,true) as $key => $value) 
    { 
    echo $key . " : " . $value['market_price'] . "<br/>"; 
    $req = "update items set opskins_suggested = '".($value['market_price']/100)."' where item_name = '".addslashes($key)."'"; 
     mysqli_query($link,$req); 
} 
sleep(10); 
} 

這裏是腳本的NodeJS:

所有的
var OPSkinsAPI = require('@opskins/api'); 
var opskins = new OPSkinsAPI(my opskins api key); 
var item = process.argv[2].split(","); 
opskins.getSuggestedPrices(730, item, function(err,price){ 


    console.log(price); 

}); 
+0

請提供您的錯誤信息。 – Corentin

回答

0

首先,這是爲了實現自己的目標非常低效的方式。如果您的Node應用程序正在偵聽某個端口並從您的PHP應用程序爲請求(HTTP或某些本地IPC)提供了答案,那將會非常有效。話雖如此,如果你仍然想運行你的應用程序的節點作爲你現在要做的,你應該在節點處理錯誤:

var OPSkinsAPI = require('@opskins/api'); 
var opskins = new OPSkinsAPI(my opskins api key); 
var item = process.argv[2].split(","); 
opskins.getSuggestedPrices(730, item, function(err,price){ 
    // handle errors: 
    if (err) { 
     console.error('Error:', err); 
     process.exit(1); 
    } 
    console.log(price); 
}); 

這樣,你就能知道發生了什麼。否則你的程序只會在stdout上返回字符串"undefined",你的PHP程序不會知道出了什麼問題。

您還需要處理PHP中的錯誤。