Skip to content

Instantly share code, notes, and snippets.

@sirkitree
Last active December 24, 2018 15:31
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save sirkitree/5129947 to your computer and use it in GitHub Desktop.
Save sirkitree/5129947 to your computer and use it in GitHub Desktop.
Parse a PHP file with Node.js and convert an array defined in the PHP script into a JSON object which the Node app can use.
var runner = require('child_process');
runner.exec(
'php -r \'include("settings.php"); print json_encode($databases);\'',
function (err, stdout, stderr) {
var connection = JSON.parse(stdout).default.default;
console.log(connection.database);
// result botdb
}
);
<?php
$databases['default']['default'] = array(
'driver' => 'mysql',
'database' => 'botdb',
'username' => 'botuser',
'password' => 'botpass',
'host' => 'localhost',
'prefix' => '',
);
@sirkitree
Copy link
Author

@ichiriac
Copy link

ichiriac commented Mar 26, 2017

Hi @sirkitree,

Instead using child_process, you could also use this js library that can parse php - may be more rapid, use less memory and avoid php dependency (+ could be syntax errors tolerant)

@malikzee001
Copy link

malikzee001 commented Dec 10, 2018

I'm working on this too. But I got error 'php' is not recognized as an internal or external command, Then I add php path in environmental variables, still same error. Any help...?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment