Last active
June 27, 2026 09:40
-
-
Save andyg2/00b8106d9d46905fed3cb28dffff0a17 to your computer and use it in GitHub Desktop.
Claude MAX via 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
| <?php | |
| /** | |
| * Run a prompt through the Claude Code CLI on Windows and return its text output. | |
| * | |
| * @param string $prompt The prompt text to send to Claude. | |
| * @param string $claudeExePath Full path to claude.exe. | |
| * @return string Claude's response (stdout + stderr combined). | |
| */ | |
| function runClaudePrompt(string $prompt, string $claudeExePath): string { | |
| // ------------------------------------------------------------------ | |
| // 1. Write the prompt to a temporary file. | |
| // tempnam() creates a uniquely-named file in the system temp dir | |
| // (e.g. C:\Users\you\AppData\Local\Temp\claude_A1B2.tmp) and returns | |
| // its path. The 'claude_' prefix just makes the file easy to spot. | |
| // ------------------------------------------------------------------ | |
| $tmpFile = tempnam(sys_get_temp_dir(), 'claude_'); | |
| file_put_contents($tmpFile, $prompt); | |
| // ------------------------------------------------------------------ | |
| // 2. Clear Claude's "I'm running inside a session" environment variables. | |
| // When Claude Code launches a child process it sets these. If they're | |
| // present, the CLI thinks it's nested inside an existing session and | |
| // behaves differently. Setting them to empty (Windows `set VAR=`) | |
| // forces a clean, top-level CLI run. | |
| // ------------------------------------------------------------------ | |
| $clearEnv = [ | |
| 'set CLAUDE_CODE_SESSION=', | |
| 'set CLAUDE_CODE=', | |
| 'set CLAUDECODE=', | |
| ]; | |
| // ------------------------------------------------------------------ | |
| // 3. Move into the temp directory. | |
| // `cd /d %TEMP%` changes both the drive and the directory (the /d flag | |
| // is what allows switching drives, e.g. from D: back to C:). | |
| // %TEMP% is expanded by cmd.exe at runtime, not by PHP. | |
| // ------------------------------------------------------------------ | |
| $changeDir = 'cd /d %TEMP%'; | |
| // ------------------------------------------------------------------ | |
| // 4. Build the claude.exe invocation with its flags. | |
| // ------------------------------------------------------------------ | |
| $claudeCommand = implode(' ', [ | |
| '"' . $claudeExePath . '"', // quoted so spaces in the path are safe | |
| '-p', // print mode: run once, print result, exit | |
| '--output-format text', // plain text out (not JSON / streaming) | |
| '--no-session-persistence', // don't save this run to session history | |
| '--allowedTools ""', // empty allow-list = no tools may run | |
| '--dangerously-skip-permissions', // don't prompt for permission on actions | |
| ]); | |
| // ------------------------------------------------------------------ | |
| // 5. Feed the prompt file in on stdin and merge stderr into stdout. | |
| // `< file` -> sends the file's contents to the program's stdin | |
| // `2>&1` -> redirects stderr (2) into stdout (1) so errors are | |
| // captured in $output too, not lost. | |
| // escapeshellarg() safely quotes the filename for the shell. | |
| // ------------------------------------------------------------------ | |
| $redirects = '< ' . escapeshellarg($tmpFile) . ' 2>&1'; | |
| // ------------------------------------------------------------------ | |
| // 6. Stitch the whole command line together. | |
| // The parts are joined with ` && ` so each step only runs if the | |
| // previous one succeeded: | |
| // | |
| // set ... && set ... && set ... && cd /d %TEMP% && "claude.exe" <flags> < tmpFile 2>&1 | |
| // ------------------------------------------------------------------ | |
| $fullCommand = implode(' && ', array_merge( | |
| $clearEnv, | |
| [$changeDir, $claudeCommand . ' ' . $redirects] | |
| )); | |
| // ------------------------------------------------------------------ | |
| // 7. Run it and capture everything it prints. | |
| // ------------------------------------------------------------------ | |
| $output = shell_exec($fullCommand); | |
| // ------------------------------------------------------------------ | |
| // 8. Clean up the temp file (the original snippet left these behind). | |
| // ------------------------------------------------------------------ | |
| @unlink($tmpFile); | |
| return $output ?? ''; | |
| } | |
| $response = runClaudePrompt('whassup claude', 'C:\\Users\\you\\.local\\bin\\claude.exe'); | |
| // Hey Andy! Not much on my end — what's up with you? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment