PHP code example of codechap / yii3-claude-code

1. Go to this page and download the library: Download codechap/yii3-claude-code 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/ */

    

codechap / yii3-claude-code example snippets


use Codechap\Yii3ClaudeCode\ClaudeCodeInterface;

final class MyController
{
    public function __construct(
        private readonly ClaudeCodeInterface $claude,
    ) {}

    public function actionAsk(): string
    {
        $response = $this->claude->query('What is the capital of France?');

        return $response->getResult();
    }
}

// config/params.php
return [
    'codechap/yii3-claude-code' => [
        'apiKey' => 'sk-ant-...',
    ],
];

$response = $claude
    ->withApiKey('sk-ant-...')
    ->query('Hello');

return [
    'codechap/yii3-claude-code' => [
        // Path to the claude binary. Empty string = auto-detect via PATH.
        'binaryPath' => '',

        // Default model: 'sonnet', 'opus', or 'haiku'.
        'model' => 'sonnet',

        // Default system prompt sent with every query.
        'systemPrompt' => '',

        // Maximum number of agentic turns (null = unlimited).
        'maxTurns' => null,

        // Tools the CLI is allowed to use.
        'allowedTools' => [],

        // Process timeout in seconds.
        'timeout' => 300,

        // Environment variables to unset in the subprocess (recursion prevention).
        'envUnset' => ['CLAUDECODE', 'ANTHROPIC_API_KEY'],

        // Anthropic API key (null = use subscription auth).
        'apiKey' => null,

        // Custom environment variables passed to the subprocess.
        'envSet' => [],
    ],
];

use Codechap\Yii3ClaudeCode\Model;

$response = $claude
    ->withModel(Model::Opus)
    ->query('Explain quantum computing');

$response = $claude
    ->withJson()
    ->query('List 3 colors as a JSON array');

$array = $response->toArray(); // Decoded JSON

// Start a conversation
$r1 = $claude
    ->withJson()
    ->query('What is the capital of France?');

// Continue with session ID
$r2 = $claude
    ->withJson()
    ->withSessionId($r1->getSessionId())
    ->query('And Germany?');

// Or continue the most recent conversation
$r3 = $claude
    ->withContinue()
    ->query('What about Italy?');

$response = $claude
    ->withSystemPrompt('Reply in haiku form only.')
    ->query('Describe the ocean');

$response = $claude
    ->withAllowedTools(['Read', 'Grep', 'Glob'])
    ->query('Find all TODO comments in this project');

$response = $claude
    ->withWorkingDirectory('/path/to/project')
    ->query('Describe this codebase');

$response = $claude
    ->withEnv([
        'HOME' => '/home/myuser',
        'PATH' => '/usr/local/bin:/usr/bin:/bin',
    ])
    ->query('Hello');

// Boolean flag (no value)
$response = $claude
    ->withFlags(['--dangerously-skip-permissions'])
    ->query('Delete all temp files');

// Single-value flag
$response = $claude
    ->withFlags(['--effort' => 'high'])
    ->query('Refactor this module');

// Multi-value flag
$response = $claude
    ->withFlags(['--add-dir' => ['/data', '/config']])
    ->query('Analyze these directories');

// Mix all three forms
$response = $claude
    ->withFlags([
        '--dangerously-skip-permissions',
        '--effort' => 'high',
        '--add-dir' => ['/data', '/config'],
    ])
    ->query('Go wild');

$response = $claude
    ->withTimeout(600) // 10 minutes
    ->query('Refactor this entire module');

$claude->query('Hello', function (Response $response): void {
    logger()->info('Claude responded', [
        'session' => $response->getSessionId(),
        'elapsed' => $response->getElapsedSeconds(),
    ]);
});

'binaryPath' => '/home/myuser/.npm-global/bin/claude',

'binaryPath' => '/home/deploy/.npm-global/bin/claude',
'envSet' => ['HOME' => '/home/deploy'],