EffakT Posted March 28, 2014 Share Posted March 28, 2014 (edited) So, I am working on my assessment for class, and I am working on a script that validates Node.js code, but it seems that it is running extremely slow on the first time the port is opened. What it should do is create a js file containing the user's input, Run the script in Node.JS, Run a cURL on the script, Echo the output of the cURL, Close Node.JS (closing the open port) Delete the user's script. My php is as follows: <?php $a = $_GET['a']; chdir('scripts'); $a = json_decode($a); $time = round(microtime(true) * 1000); $data = ""; $file = $time.'.js'; $handle = fopen($file, 'w') or die('Cannot open file: '.$file); foreach ($a as $b) { $data .= $b."\r\n"; } preg_match('/(listen\s*\((\d*)\))/', $data, $port); $port = $port[count($port)-1]; fwrite($handle, $data); fclose($handle); $h = popen('"C:\Program Files\nodejs\node.exe" '.$file, 'r'); sleep(1); exec('C:\curl '.$_SERVER['HTTP_HOST'].':'.$port, $output); foreach($output as $out) { echo $out."\r\n"; } pclose($h); unlink($file); ?> Does anybody know of any other way to do this that would reduce this lag?Also, it seems that the pclose($h); isn't stopping node properly. Thanks --EffakT Edited March 28, 2014 by EffakT Quote Link to comment https://forums.phpfreaks.com/topic/287359-php-shell-execution/ Share on other sites More sharing options...
gristoi Posted March 28, 2014 Share Posted March 28, 2014 isnt that meant to be part of your learning process? going and resourcing the information to overcome this. The developers in this forum will happily help you overcome a problem, but not do your homework for you Quote Link to comment https://forums.phpfreaks.com/topic/287359-php-shell-execution/#findComment-1474309 Share on other sites More sharing options...
EffakT Posted March 28, 2014 Author Share Posted March 28, 2014 I have been doing research on it and came up blank, I have no clue how to do it, so I was asking for some help/support. Quote Link to comment https://forums.phpfreaks.com/topic/287359-php-shell-execution/#findComment-1474357 Share on other sites More sharing options...
EffakT Posted March 28, 2014 Author Share Posted March 28, 2014 Okay, I managed to get it closing node.exe, now I just need it to not lag as much on loading Node.JS, but it seems to be logging the error: PHP Fatal error: Maximum execution time of 30 seconds exceeded in executeScript.php on line 20 New Code: <?php $a = $_GET['a']; chdir('scripts'); $a = json_decode($a); $time = round(microtime(true) * 1000); $data = ""; $file = $time.'.js'; $handle = fopen($file, 'w') or die('Cannot open file: '.$file); foreach ($a as $b) { $data .= $b."\r\n"; } preg_match('/(listen\s*\((\d*)\))/', $data, $port); $port = $port[count($port)-1]; fwrite($handle, $data); fclose($handle); error_log("Launching Node"); shell_exec('"C:\Program Files\nodejs\node.exe" '.$file); error_log("Running cURL"); exec('C:\curl '.$_SERVER['HTTP_HOST'].':'.$port, $output); error_log("killing node"); exec('taskkill /F /IM node.exe'); foreach($output as $out) { echo $out."\r\n"; } unlink($file); ?> Quote Link to comment https://forums.phpfreaks.com/topic/287359-php-shell-execution/#findComment-1474364 Share on other sites More sharing options...
boompa Posted March 29, 2014 Share Posted March 29, 2014 Well, instead of exec-ing cURL here: exec('C:\curl '.$_SERVER['HTTP_HOST'].':'.$port, $output); I would consider using PHP's cURL library. Quote Link to comment https://forums.phpfreaks.com/topic/287359-php-shell-execution/#findComment-1474385 Share on other sites More sharing options...
Solution EffakT Posted March 29, 2014 Author Solution Share Posted March 29, 2014 Sweet, thanks boompa, that wasn't but problem but actually helped a lot! I run the node script in the background (this doesn't slow down my script), then run a cURL and then close node, if cURL returns an error, I run node normally since it will return an error and wont execute. This is so I can get the error that node returns. Quote Link to comment https://forums.phpfreaks.com/topic/287359-php-shell-execution/#findComment-1474401 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.