PHP Classes
elePHPant
Icontem

PHP Node.js: Execute JavaScript using Node.js from PHP

Recommend this page to a friend!
  Info   View files View files (3)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2013-07-11 (3 years ago) RSS 2.0 feedStarStarStarStar 64%Total: 1,433 This week: 1All time: 2,648 This week: 601Up
Version License PHP version Categories
php-nodejs 1.0BSD License5.0PHP 5, Emulators
Description Author

This class can execute JavaScript using Node.js from PHP.

It can take a given JavaScript source file and executes it by running the Node.js program.

The class changes the current directory to the path of the given JavaScript file.

Optionally it can output debug messages, pass an array of parameters to JavaScript and pre-load jQuery.

Innovation Award
PHP Programming Innovation award nominee
July 2013
Number 3


Prize: One downloadable copy of PhpED Professional
Node.js is runtime engine that can execute given JavaScript code.

This class simplifies the execution of JavaScript code started from a PHP script by calling the Node.js runtime engine passing given parameters from PHP.

Manuel Lemos
  Performance   Level  
Name: Dean Gostiša <contact>
Classes: 1 package by
Country: Slovenia Slovenia
Age: 27
All time rank: 18286 in Slovenia Slovenia
Week rank: 479 Up1 in Slovenia Slovenia Up
Innovation award
Innovation award
Nominee: 1x

Details
PHPNodeJS
=========

PHP Node JS wrapper - PHP class that brings JavaScript executing inside PHP - wrapper for NodeJS CLI.  
  
This class was made that we can use JavaScript based validators of tasks (interacitve tasks) for competition system Bober (programming competition). 
JavaScript server execution enables us to protect to correct solution for tasks.  
  
We think that this solution could be used for many other things so we published this wrapper online, so you can review it, test it and make it better.

Requirements
------------
- [NodeJS]("http://nodejs.org/", "NodeJS") (>= v0.10.10, maybe it works with older version (never tested, you are welcome to test and report if it works or not), there must be binary file "node" on server / computer where you execute script)
- If you want to use JavaScript function that uses jQuery you need to install jquery for NodeJS via NPM (in directory where 
PHPNodeJS.php is located in your project, you need to run command in CLI: `npm install jquery`

Initialize
----------
```
include_once dirname(__FILE__) . '/PHPNodeJS.php';
// if $debug = true all of debug messages will be shown, otherwise not
$debug = true;
$PHPNodeJS = new PHPNodeJS($debug);
```

Example 1
--------
Run simple script without function
```
echo $PHPNodeJS->run('
  var var1 = 10;
  var var2 = 20;
  console.log(var1+var2);
');
```

Example 2 
---------
Run simple script, supply multiple arguments to function you want to call
```
echo $PHPNodeJS->run('
  function test(var1, var2) {
    if (var1 == "3" && var2 == "20") {
      return true;
    } else {
      return false;
    }
  }',
  'test', array('3', '20'));
```

Example 3
---------
Run script that uses jQuery as dependcy
```
echo $PHPNodeJS->run('
  function test(url) {
   jQuery.get(url, function(data) {
     console.log(data);
   });
   return "";
  }',
  'test', array('http://www.videodeck.net'), true);
```
License
-------
This project is licensed under [Creative Commons Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0)]("http://creativecommons.org/licenses/by-sa/3.0/", "CC BY-SA 3.0"). You are welcome to contribute to this project or 
fork this product but we would be very happy that you keep reference to this original project.
  Files folder image Files  
File Role Description
Plain text file PHPNodeJS.php Class Class source
Accessible without login Plain text file PHPNodeJSExample.php Example Example script
Accessible without login Plain text file README.md Doc. Documentation

 Version Control Unique User Downloads Download Rankings  
 100%
Total:1,433
This week:1
All time:2,648
This week:601Up
 User Ratings  
 
 All time
Utility:85%StarStarStarStarStar
Consistency:78%StarStarStarStar
Documentation:67%StarStarStarStar
Examples:75%StarStarStarStar
Tests:-
Videos:-
Overall:64%StarStarStarStar
Rank:743