Skip to content

Copilot PHP API

ext-php-copilot exposes GitHub Copilot to PHP 8.3 applications through two API layers:

  • ExtPhpCopilot\Copilot: a Composer-friendly wrapper for application code.
  • Copilot\Client and Copilot\Session: native extension classes for direct JSON-RPC access.

Prefer the wrapper for applications, plugins, workers, and CLI commands. Use the native API when you need direct control over JSON payloads, streaming event polling, or SDK-specific options.

DocumentDescription
COPILOT-WRAPPER.mdWrapper API reference for ExtPhpCopilot\CopilotConfig and ExtPhpCopilot\Copilot.
COPILOT-NATIVE.mdNative extension reference for copilot_sdk_version(), Copilot\Client, and Copilot\Session.
COPILOT-OPTIONS.mdJSON option reference for clients, transports, telemetry, sessions, messages, and model changes.
COPILOT-EXAMPLES.mdRunnable examples, acceptance test flow, and common usage recipes.
Use caseRecommended API
Generic PHP app integrationExtPhpCopilot\Copilot
Token-based web application authExtPhpCopilot\Copilot::fromEnvironment()
Local CLI development with existing login stateExtPhpCopilot\Copilot::forCliUser()
Model listing, raw status, or SDK method callsCopilot\Client
Streaming events or manual session controlCopilot\Session
use ExtPhpCopilot\Copilot;
$copilot = Copilot::fromEnvironment(
cwd: getcwd(),
copilotHome: __DIR__ . '/../var/copilot'
);
try {
$copilot->assertAuthenticated();
$event = $copilot->ask('Explain this project in one paragraph.');
} finally {
$copilot->close();
}
$client = new Copilot\Client(json_encode([
'githubToken' => getenv('GITHUB_COPILOT_TOKEN'),
'useLoggedInUser' => false,
'copilotHome' => __DIR__ . '/../var/copilot',
], JSON_THROW_ON_ERROR));
$session = $client->createSession(json_encode([
'permissionPolicy' => 'deny_all',
], JSON_THROW_ON_ERROR));
$eventJson = $session->sendAndWaitJson('Explain this project in one paragraph.');
$session->disconnect();
$client->stop();

For server-side PHP applications, use explicit token auth and isolated Copilot CLI state.

RequirementRecommendation
Token storageStore GITHUB_COPILOT_TOKEN in the environment or a secret manager.
CLI user fallbackKeep useLoggedInUser disabled for web servers.
CLI stateSet an app-owned copilotHome outside the web root.
Tool permissionsUse permissionPolicy set to deny_all unless the app intentionally grants tools.