Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/longbill/node-phpfpm
node.js run php scripts via phpfpm
https://github.com/longbill/node-phpfpm
nodejs php php-fpm
Last synced: 3 months ago
JSON representation
node.js run php scripts via phpfpm
- Host: GitHub
- URL: https://github.com/longbill/node-phpfpm
- Owner: longbill
- License: mit
- Created: 2015-12-29T02:50:00.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2024-06-03T14:14:54.000Z (5 months ago)
- Last Synced: 2024-07-23T13:35:22.286Z (4 months ago)
- Topics: nodejs, php, php-fpm
- Language: JavaScript
- Size: 13.7 KB
- Stars: 52
- Watchers: 9
- Forks: 14
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# node-phpfpm
node.js run php scripts via phpfpm[![NPM](https://nodei.co/npm/node-phpfpm.png?downloads=true&stars=true)](https://www.npmjs.com/package/node-phpfpm)
```
npm install node-phpfpm
```## Usage
```js
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
```js
var phpfpm = new PHPFPM(configObject);
```configObject may have the following keys:
* `documentRoot` optional [string] the document root folder of PHP scripts. must end 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 sent with content-type: application/x-www-form-urlencoded
* `json` optional [object] json data that will be sent 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 headerif you send a string as `options`, it will be converted to:
```js
{ uri: "the string value", method: 'GET' }
```callback
```js
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
```js
phpfpm.run('test1.php', function(err, output, phpErrors)
{
console.log(err, output, phpErrors);
});
```Send data via GET method
```js
phpfpm.run('test.php?a=b&c=d&e[0]=1&e[1]=2', function(err, output, phpErrors)
{
console.log(err, output, phpErrors);
});
```
```php
b
// [c] => d
// [e] => Array
// (
// [0] => 1
// [1] => 2
// )
// )
?>
```Send form data via POST method
```js
phpfpm.run(
{
uri: 'test.php',
form:
{
a:'a',
b:'b'
}
}, function(err, output, phpErrors)
{
console.log(err, output, phpErrors);
});
```
```php
a
// [b] => b
// )
?>
```Send json data with POST method
```js
phpfpm.run(
{
uri: 'test.php',
json:
{
a:'a',
b:'b'
}
}, function(err, output, phpErrors)
{
console.log(err, output, phpErrors);
});
```
```php```
Send form data with GET method
```js
phpfpm.run(
{
uri: 'test2.php',
method: 'GET',
form:
{
a:'a',
b:'b'
}
}, function(err, output, phpErrors)
{
console.log(err, output, phpErrors);
});
```
```php
a
// [b] => b
// )
?>
```Send form data and query string with GET method
```js
phpfpm.run(
{
uri: 'test2.php?c=cc',
method: 'GET',
form:
{
a:'a',
b:'b'
}
}, function(err, output, phpErrors)
{
console.log(err, output, phpErrors);
});
```
```php
cc
// [a] => a
// [b] => b
// )
?>
```Send raw body data with POST method
```js
phpfpm.run(
{
uri: 'test5.php',
body: 'abc123'
}, function(err, output, phpErrors)
{
console.log(err, output, phpErrors);
});
```
```php```
## License
MIT## Thanks
This project is based on the great work of `node-fastcgi-client` written by LastLeaf. [LastLeaf/node-fastcgi-client](https://github.com/LastLeaf/node-fastcgi-client)
## Links
[How to execute PHP scripts with Node.JS](http://jszen.com/how-to-execute-php-scripts-with-nodejs.8.html)