Skip to content

Instantly share code, notes, and snippets.

@masakielastic
Last active July 4, 2025 16:29
Show Gist options
  • Save masakielastic/e3123904678e7b1c59b29695f71d52ed to your computer and use it in GitHub Desktop.
Save masakielastic/e3123904678e7b1c59b29695f71d52ed to your computer and use it in GitHub Desktop.
php-node を使って CLI で PHP スクリプトを実行する
import { Php, Request } from '@platformatic/php-node';
import path from 'path';
import fs from 'fs';
const phpScript = process.argv[2];
if (!phpScript) {
console.error('Usage: node cli.js <php-script>');
process.exit(1);
}
// ファイル存在チェック(カレントディレクトリ以外も可)
const absPath = path.isAbsolute(phpScript) ? phpScript : path.resolve(process.cwd(), phpScript);
if (!fs.existsSync(absPath)) {
console.error(`File not found: ${absPath}`);
process.exit(1);
}
const php = new Php({
argv: process.argv,
docroot: path.dirname(absPath) // スクリプトのあるディレクトリをdocrootに
});
// URL の path 部分のみを指定
const url = 'http://example.com/' + path.basename(absPath);
const request = new Request({
url
});
const response = await php.handleRequest(request);
console.log(response.body.toString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment