Node. js-How to Use php to execute nodejs files

Source: Internet
Author: User
How can I use php exec to execute nodejs files? The following execution fails. {code ...} if only exec (& quot; node-v $ serverPath & quot;, $ info, $ val); is used, the version number can be output and the execution is successful. Thank you for your answers. How can I use php exec to execute nodejs files? The following execution fails.

        public function execTask ($dirPath)        {            $serverPath = self::$serverPath;            exec("node httpd.js $serverPath",$info, $val);            var_dump($info);            echo "$val";        }

If onlyexec("node -v $serverPath",$info, $val);But the version number can be output and the execution is successful. Thank you for your answers.

Reply content:

How can I use php exec to execute nodejs files? The following execution fails.

        public function execTask ($dirPath)        {            $serverPath = self::$serverPath;            exec("node httpd.js $serverPath",$info, $val);            var_dump($info);            echo "$val";        }

If onlyexec("node -v $serverPath",$info, $val);But the version number can be output and the execution is successful. Thank you for your answers.

You can try shell_exec.

I also have similar usage, so in addition to reading messages, there are also functions that can be executed directly in the background, all implemented through processes.

/*** The returned Chinese text is GB2312 encoded in windows and needs to be manually changed to UTF8 * iconv ("GB2312", "UTF-8", $ read ); * @ param $ cmd * @ return array */public function execFront ($ cmd) {$ response = array (); $ handle = popen ("$ cmd 2> & 1 ", 'R'); while ($ read = fread ($ handle, 20096) {if (! Mb_detect_encoding ($ read, 'utf-8', true) iconv ("GB2312", "UTF-8", $ read); $ response [] = trim ($ read );} pclose ($ handle); flush (); return $ response;}/*** @ param $ cmd */public function execBackend ($ cmd) {if (substr (php_uname (), 0, 7) = "Windows") {pclose (popen ("start cmd/c ". $ cmd, "r");} else {exec ($ cmd. ">/dev/null &");} return ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.