npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

phplike

v2.5.12

Published

Implement php function for nodejs

Downloads

163

Readme

Node.js - phplike

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.

  • npm: https://npmjs.org/package/phplike
  • All version: https://registry.npmjs.org/phplike
  • Git Source: https://github.com/puritys/nodejs-phplike

Travis CI status: Unit testing Coverage Status

Install phplike

  • sudo npm install -g phplike

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.

  • sudo npm install -g node-gyp
   var php = require("phplike/module");
   var dirs = php.exec("ls ./");
   php.print_r(dirs);

How many OS does phplike support?

| 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.

Dependency

  • Phplike have been already tested in Node.js version from 0.10.x to 0.12.x and io.js 1.0.0 to 2.1.0. Here is the test report: https://travis-ci.org/puritys/nodejs-phplike
  • libcurl (libcurl-7.19) : Linux system already has this built-in package. Please install libcurl-devel : sudo yum install libcurl-devel
  • python 2.4 ~ : phplike use node-gyp to compile C/C++ codes. It needs python with the version must be bigger than 2.7.8 , you can download python from here https://www.python.org/downloads/.

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.

Completed PHP Method

System function

  • exec : Execute an external program
  • system
  • curl : curl_init, curl_setopt, curl_exec, curl_close, reuqest ( HTTP sync )
  • usleep , sleep

basic

  • print_r
  • is_string
  • is_int
  • is_object
  • is_array
  • is_numeric
  • is_int
  • isset
  • empty
  • exit
  • explode
  • implode

File Handler

  • file_get_contents
  • file_put_contents
  • mkdir
  • unlink
  • rmdir (dirname, isForce)
  • is_file, is_dir
  • readdir (get all file list in select directory)

Encoder and Decoder

  • json_encode, json_decode, handle multibyte: json_decode(xx, 'JSON_UNESCAPED_UNICODE')
  • md5
  • base64_encode
  • base64_decode

String

  • sprintf
  • str_pad
  • substr (string, start, length)
  • strtolower
  • strtoupper

XML Parser XML Parser Document

  • DOMDocument

  • getElementsByTagName

  • DOMElement

  • firstChild

  • lastChild

  • hasAttributes

Socket

  • fsockopen
  • sendcmd
  • fwrite
  • fread

Mysql

  • mysqli_connect
  • mysql_connect
  • mysql_select_db
  • mysql_query
  • mysql_close
  • mysql_create_db
  • mysql_insert_id

array

  • shuffle
  • array_merge
  • array_rand

Others

  • time, date, mktime
  • chr, ord : string to ascii number, ascii number to string
  • decbin, bindec
  • parse_str : parse "a=b&c=d" to {"a": "b", "c": "d"}
  • clone: clone a object or array
  • getcwd
  • urlencode, urldecode
  • intval: convert string to integer
  • strval: convert integer to string
  • trim
  • http_build_query

Execute phplike in global mode sample

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);

Execute phplike in module mode sample (phplike 2.0)

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);

Example code for php curl

    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);

Example code for php post (Using module mode)

    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

Example code for making a blocking request

    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);

Functions will be implemented in the future

  • abs
  • acos

phplike Development

  • Visual studio: Windows will need this software in order to compile C/C++ code, you can download from here http://www.visualstudio.com/zh-tw/downloads/download-visual-studio-vs#DownloadFamilies_4 . Notice! If you install vs2010 Express, it only support 32bit, so you should install the 32bit version Node.js too.