1. Go to this page and download the library: Download beyondcode/claude-hooks-sdk library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
beyondcode / claude-hooks-sdk example snippets
eyondCode\ClaudeHooks\ClaudeHook;
// Read the hook data from stdin.
// This will automatically return the correct Hook instance (for example PreToolUse)
$hook = ClaudeHook::create();
// Example: Validate bash commands
if ($hook->toolName() === 'Bash') {
$command = $hook->toolInput('command', '');
// Check for potentially dangerous commands
if (str_contains($command, 'rm -rf')) {
// Block the tool call with feedback
$hook->response()->block('Dangerous command detected. Use caution with rm -rf commands.');
}
}
// Allow other tool calls to proceed
$hook->success();
use BeyondCode\ClaudeHooks\ClaudeHook;
use BeyondCode\ClaudeHooks\Hooks\{PreToolUse, PostToolUse, Notification, Stop, SubagentStop};
$hook = ClaudeHook::create();
if ($hook instanceof PreToolUse) {
$toolName = $hook->toolName(); // e.g., "Bash", "Write", "Edit"
$toolInput = $hook->toolInput(); // Full input array
$filePath = $hook->toolInput('file_path'); // Specific input value
}
if ($hook instanceof PostToolUse) {
$toolResponse = $hook->toolResponse(); // Full response array
$success = $hook->toolResponse('success', true); // With default value
}
if ($hook instanceof Notification) {
$message = $hook->message();
$title = $hook->title();
}
if ($hook instanceof Stop || $hook instanceof SubagentStop) {
$isActive = $hook->stopHookActive();
}
// Continue processing (default behavior)
$hook->response()->continue();
// Stop Claude from continuing with a reason
$hook->response()->stop('Reason for stopping');
// For PreToolUse: approve or block tool calls
$hook->response()->approve('Optional approval message')->continue();
$hook->response()->block('Required reason for blocking')->continue();
// Suppress output from transcript mode
$hook->response()->suppressOutput()->continue();