Run PHP program locally from browser using Node.js

Node.js is a link that allows you to connect your browser to executable files on your local computer.

The following diagram shows the operation of the system:

  1. Run Node with server script:
    node runphp.js
  2. In
  3. enter the local URL of the script in the browser:
    localhost:1000/dirlist.php
  4. The node is starting script execution.
  5. The script displays the result using the echo command.
  6. The result is sent by Node to a new HTML page.

PHP HTML et Node.js

To start the program, enter the name of the program in the URL string:

localhost:1000/dirlist.php

And you can also go from variables to a program that needs to be executed in this form:

localhost:1000/dirlist.php?x=test 

The server's JavaScript code comes from code that has already been used to create an HTML file server with Node.js. But this time, instead of displaying content, the child_process module is used to launch the program, then the results of the program will be displayed.
This works both at the local post and at the remote hosting.

1) As always, you start with creating a server

var server = http.createServer(php);
server.listen(1000);
console.log("PHP ready to run script given on port 1000.");

And we assign a communication port.

2) Check for script in the file system

function php(request, response)
{
  var urlpath = url.parse(request.url).pathname;
  var param = url.parse(request.url).query;
  var localpath = path.join(process.cwd(), urlpath);
  fs.exists(localpath, function(result) { runScript(result, localpath, param, response)});
}

The file name is retrieved from the URL using the parse method of the url module.
And additionally, we extract the parameter string using the query method. It is passed as is to the PHP script.

3) Run the script

function runScript(exists, file, param, response)
{
  if(!exists) return sendError(404, 'File not found', response);
  runner.exec("php " + file + " " + param,
   function(err, stdout, stderr) { sendData(err, stdout, stderr, response); });
}

The exec method from child_process to the first parameter, which is entered on the command line, so the PHP interpreter name, script name, and parameter list.

4) Everything that the script displays will be displayed in the browser

function sendData(err, stdout, stderr, response)
{
  if (err) return sendError(500, stderr, response);
  response.writeHead(200,{"Content-Type": "text/plain;charset=utf-8"});
  response.write(stdout);
  response.end();
}

This is the role of the sendData function called as a callback by the exec method.

5) Example PHP script

This demo PHP script reads files from the directory and displays a list of them. Everything it displays will appear in the browser.

<?php
echo "Parameter:".$argv[1]."\n";
echo "Directory content...\n\n";

$output="";
if ($hnd = opendir('.'))
{
  while($file = readdir($hnd))
  {
    if ($file == "." || $file == "..") continue;
    $output .= "$file\n";
  }
  closedir($hnd);
}
echo $output;
?>

The parameters are in the $ argv table. The first parameter (the only one in the example) in $ argv [1]. The programmer must cut the line and use its contents.

6) Full JavaScript source code

var http = require("http"),
path = require("path"),
fs = require("fs"),
url = require("url"),
runner = require("child_process");

function sendError(errCode, errString, response)
{
  response.writeHead(errCode, {"Content-Type": "text/plain;charset=utf-8"});
  response.write(errString + "\n");
  response.end();
  return false;
}

function sendData(err, stdout, stderr, response)
{
  if (err) return sendError(500, stderr, response);
  response.writeHead(200,{"Content-Type": "text/plain;charset=utf-8"});
  response.write(stdout);
  response.end();
}

function runScript(exists, file, param, response)
{
  if(!exists) return sendError(404, 'File not found', response);
  runner.exec("php " + file + " " + param,
   function(err, stdout, stderr) { sendData(err, stdout, stderr, response); });
}

function php(request, response)
{
  var urlpath = url.parse(request.url).pathname;
  var param = url.parse(request.url).query;
  var localpath = path.join(process.cwd(), urlpath);
  fs.exists(localpath, function(result) { runScript(result, localpath, param, response)});
}

var server = http.createServer(php);
server.listen(1000);
console.log("PHP ready to run script given on port 1000.");

Local server

In fact, we have a local application server base with this program: by changing the script name in the URL string, you can run different tools.
To automate the launch, you can put each in bookmark, and possibly use bookmarklets to transfer settings.
This local server can also be started itself by placing the above command in a batch file that will be executed when the session starts.