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

node-phpfpm

v1.0.1

Published

nodejs run php scripts via phpfpm

Downloads

2,398

Readme

node-phpfpm

node.js run php scripts via phpfpm

NPM

npm install node-phpfpm

Usage

var PHPFPM = require('node-phpfpm');

var phpfpm = new PHPFPM(
{
	host: '127.0.0.1',
	port: 9000,
	documentRoot: __dirname
});

phpfpm.run('test.php', function(err, output, phpErrors)
{
	if (err == 99) console.error('PHPFPM server error');
	console.log(output);
	if (phpErrors) console.error(phpErrors);
});

Configuration

var phpfpm = new PHPFPM(configObject);

configObject may have the following keys:

  • documentRoot optional [string] the document root folder of PHP scripts. must ends with /
  • host optional [string] the ip or host name of php-fpm server (default: 127.0.0.1)
  • port optional [int] the port of php-fpm server ( default: 9000 )
  • sockFile optional [string] use the unix sock file instead of 127.0.0.1:9000 to connect php-fpm server

APIs

run(options, callback)

available keys in options object

  • uri [string] path to your phpfile
  • url [string] alias of uri
  • method optional [string] GET or POST (default: GET)
  • form optional [object] form_data that will be send with content-type: application/x-www-form-urlencoded
  • json optional [object] json data that will be send with content-type: application/json
  • body optional [string] raw post body data
  • contentType optional [string] the content-type header
  • contentLength optional [string] the content-length header

if you send a string as options, it will be converted to:

{ uri: "the string value", method: 'GET' }

callback

function(err, output, phpErrors)
{
	// if err === 99, means php-fpm error 
	// it may be lost php-fpm connection or too many connections
	// otherwise it will always equal to false
	
	// output is the stdout of php scripts
	
	// phpErrors is the php errors detail string
	// php will output some errors, but that does not mean the request fails
	// if you turn on display_errors in your php.ini, the phpErrors content will also be found in the output string
	
	console.log(err, output, phpErrors); 
}

Demo

Simple php request with no parameters

phpfpm.run('test1.php', function(err, output, phpErrors)
{
	console.log(err, output, phpErrors);
});

Send data via GET method

phpfpm.run('test.php?a=b&c=d&e[0]=1&e[1]=2', function(err, output, phpErrors)
{
	console.log(err, output, phpErrors);
});
<?php
print_r($_GET);
// Array
// (
//     [a] => b
//     [c] => d
//     [e] => Array
//         (
//             [0] => 1
//             [1] => 2
//         )
// )
?>

Send form data via POST method

phpfpm.run(
{
	uri: 'test.php',
	form: 
	{
		a:'a',
		b:'b'
	}
}, function(err, output, phpErrors)
{
	console.log(err, output, phpErrors);
});
<?php
print_r($_POST);
// Array
// (
//     [a] => a
//     [b] => b
// )
?>

Send json data with POST method

phpfpm.run(
{
	uri: 'test.php',
	json: 
	{
		a:'a',
		b:'b'
	}
}, function(err, output, phpErrors)
{
	console.log(err, output, phpErrors);
});
<?php
echo file_get_contents('php://input');
// {"a":"a","b":"b"}
?>

Send form data with GET method

phpfpm.run(
{
	uri: 'test2.php',
	method: 'GET',
	form: 
	{
		a:'a',
		b:'b'
	}
}, function(err, output, phpErrors)
{
	console.log(err, output, phpErrors);
});
<?php
print_r($_GET);
// Array
// (
//     [a] => a
//     [b] => b
// )
?>

Send form data and query string with GET method

phpfpm.run(
{
	uri: 'test2.php?c=cc',
	method: 'GET',
	form: 
	{
		a:'a',
		b:'b'
	}
}, function(err, output, phpErrors)
{
	console.log(err, output, phpErrors);
});
<?php
print_r($_GET);
// Array
// (
//     [c] => cc
//     [a] => a
//     [b] => b
// )
?>

Send raw body data with POST method

phpfpm.run(
{
	uri: 'test5.php',
	body: 'abc123'
}, function(err, output, phpErrors)
{
	console.log(err, output, phpErrors);
});
<?php
echo file_get_contents('php://input');
// abc123
?>

License

MIT

Thanks

This project is based on the great work of node-fastcgi-client written by LastLeaf. LastLeaf/node-fastcgi-client

Links

How to execute PHP scripts with Node.JS