Porting PHP to Node.js
Chinese Readme: https://github.com/puritys/nodejs-phplike/wiki/%E4%B8%AD%E6%96%87%E7%89%88-Readme
This project’s purpose is to provider some PHP synchronous functions for the web development on Node.js.
Node.js is a event driven language and it has many asynchronous I/O methods. Asynchronous functions is not a bad way for a web system, it helps us to develop a non-blocking I/O program. But sometimes, we want to make code readable and easier to be maintained that asynchronous functions don’t have them.
In order to reduce the number of callback functions on Node.js scripts and improve the readability. I create the library phplike which supports many synchronous functions for Node.js such as “exec”, “curl”, “fsockopen” that can be executed without complicated callback functions. In additional, phplike provides the function cUrl which has the same operations with PHP’s function called curl. It will let you make an HTTP request synchronously.
If your computer is not a normal OS, and it doesn’t have the header files of Curl, You will need to install node-gyp first. The installation of the phplike library will compile the C/C++ code with node-gyp. Usually, NPM will automatically install node-gyp when you try to install the phplike. Or you can install node-gyp by yourself.
var php = require("phplike/module");
var dirs = php.exec("ls ./");
php.print_r(dirs);
OS | Suggested phplike Version |
---|---|
Linux | Latest |
Mac | 2.0.5 ~ latest |
Windows | 2.2.8, 2.4.2 |
Raspberry PI | 2.2.2 ~ latest |
If you have any issue for installing phplike, please open a issue in anytime. I will be pleasant to help you.
After the new version of phplike 2.2.0, I committed all binary files which already compiled in Windows, Mac and Linux, you can just install the phplike without compiling C/C++ now.
System function
basic
File Handler
Encoder and Decoder
String
XML Parser
XML Parser Document
DOMDocument
DOMElement
Socket
array
Others
You can directly use phplike functions. Functions of phplike are defined at the global object when you require the index.js. It means that you don’t need the prefix before calling the phplike’s function. The Node.js coding will be more like PHP’s. The only one disadvantage is that defining a function at the global object is easier to meet the conflict problem.
exec(command, printInScreen = true);
- require("phplike");
- var tm = time();
- sleep(10);
- var result = exec("ls -la");
- print_r(result);
You can require the module.js of phplike, it will return the Phplike object. This object includes many functions of PHP for you to use at any time. The module mode will not change methods of the global object so it won’t be a conflict with other Node.js native functions.
var php = require("phplike/module.js");
var tm = php.time();
php.sleep(10);
var result = php.exec("ls -la");
php.print_r(result);
require('phplike');
var url = "https://www.google.com.tw/search?q=php+unit+test";
var header = {"Cookie": "xxx"};
var c = curl_init();
curl_setopt(c, 'CURLOPT_URL', url);
var res = curl_exec(c);
curl_close(c);
console.log("respones = " + res);
var php = require("phplike/module.js");
var url = "http://localhost:8080/";
var param = {"q": "x"};
var header = {"Cookie": "xxx"};
var c = php.curl_init();
php.curl_setopt(c, 'CURLOPT_URL', url);
php.curl_setopt(c, 'CURLOPT_POST', 1);
php.curl_setopt(c, 'CURLOPT_POSTFIELDS', "a=bbb&c=eee");
php.curl_setopt(c, 'CURLOPT_HTTPHEADER', header);
var res = php.curl_exec(c);
var responseHeader = php.getResponseHeader(); // Get header
var phplikeMod = require('phplike/module.js');
var url = "http://localhost:8080/";
var param = {"q": "x"};
var header = {"Cookie": "xxx"};
var res = phplikeMod.request("GET", url, param, header);