Last active
July 4, 2025 16:29
-
-
Save masakielastic/e3123904678e7b1c59b29695f71d52ed to your computer and use it in GitHub Desktop.
php-node を使って CLI で PHP スクリプトを実行する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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