-
Notifications
You must be signed in to change notification settings - Fork 0
nodejs calls PHP using child_process
In our project there is a use-case that we need to rely on a third-party php SDK, but since all of my projects are coded in node, I don't want to bother with setting up a php environment and then expose some http APIs for my node, I just want to leave the SDK as it is and call it directly from my node, here is how I do it.
Node's child_process avail you to fork a new process and in this process you can do heavy works such as calling another program through command line, and trigger a callback after program done.
function callPhpSDK(param){
//do some check with param
....
//the command to be executed
var cmd = 'php -f /your/path/to/SDK.php ' + param;
var child = require("child_process")
.exec(cmd, function(err, stdout, stderr){
if(err || stderr){
console.error(err || stderr);
return;
}
console.log(stdout);
});
}
php -f
file is a php command to execute a php file from command line, in the file, you can receive command line parameters from $arg_v etc.
All you need is to install php on your server, no need to set up a php server for it.
Twitter : @morfies1
Field : nodejs, react-native, redis, mysql, mongodb
Location: ShenZhen China
Email : hustfei@foxmail.com