Skip to content

Examples Reference

This page documents the bundled scripts and common usage recipes. Run examples with the debug extension loaded.

RequirementDescription
Extension buildRun cargo build before executing examples.
Extension pathmacOS uses target/debug/libext_php_copilot.dylib; Linux uses target/debug/libext_php_copilot.so; Windows uses a .dll.
AuthenticationSet GITHUB_COPILOT_TOKEN or use explicit CLI-user configuration for local development.
ScriptPurpose
examples/basic.phpDirect native client/session flow.
examples/models.phpModel listing.
examples/streaming.phpStreaming event polling.
examples/generic_app.phpRecommended wrapper flow for generic PHP apps.
Terminal window
cargo build
php -d extension=target/debug/libext_php_copilot.dylib examples/basic.php
php -d extension=target/debug/libext_php_copilot.dylib examples/models.php
php -d extension=target/debug/libext_php_copilot.dylib examples/streaming.php
php -d extension=target/debug/libext_php_copilot.dylib examples/generic_app.php

Replace the extension path with the Linux .so or Windows .dll path when running on another platform.

Use ExtPhpCopilot\Copilot when integrating Copilot into an application. This flow keeps auth, JSON handling, session lifecycle, and cleanup in one wrapper.

<?php
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
use ExtPhpCopilot\Copilot;
$copilot = Copilot::fromEnvironment(
cwd: getcwd(),
copilotHome: __DIR__ . '/../var/copilot'
);
try {
$copilot->assertAuthenticated();
$event = $copilot->ask('Explain this project in one paragraph.');
var_dump($event);
} finally {
$copilot->close();
}

Use Copilot\Client and Copilot\Session directly when you need raw JSON options or streaming/session control.

use Copilot\Client;
$client = new Client(json_encode([
'cwd' => getcwd(),
'githubToken' => getenv('GITHUB_COPILOT_TOKEN'),
'useLoggedInUser' => false,
'copilotHome' => __DIR__ . '/../var/copilot',
], JSON_THROW_ON_ERROR));
$session = $client->createSession(json_encode([
'model' => 'gpt-5',
'streaming' => true,
'permissionPolicy' => 'deny_all',
], JSON_THROW_ON_ERROR));
$event = json_decode(
$session->sendAndWaitJson('Explain this project in one paragraph.'),
true,
512,
JSON_THROW_ON_ERROR
);
$session->disconnect();
$client->stop();

Call Copilot\Client::modelsJson() to inspect available model metadata.

$client = new Copilot\Client(json_encode([
'githubToken' => getenv('GITHUB_COPILOT_TOKEN'),
'useLoggedInUser' => false,
'copilotHome' => __DIR__ . '/../var/copilot',
], JSON_THROW_ON_ERROR));
$models = json_decode($client->modelsJson(), true, 512, JSON_THROW_ON_ERROR);
print_r($models);
$client->stop();

Create a streaming session, send a prompt with immediate delivery, and poll for events.

$session = $client->createSession(json_encode([
'streaming' => true,
'permissionPolicy' => 'deny_all',
], JSON_THROW_ON_ERROR));
$session->send('Write a short haiku about PHP extensions.', json_encode([
'mode' => 'immediate',
], JSON_THROW_ON_ERROR));
while (($eventJson = $session->nextEventJson(1000)) !== null) {
$event = json_decode($eventJson, true, 512, JSON_THROW_ON_ERROR);
echo $event['type'] ?? 'event', PHP_EOL;
}

The acceptance test loads .env, verifies authentication, sends one prompt, and stores local Copilot CLI state under var/copilot-acceptance.

GITHUB_COPILOT_TOKEN=your_token_here
Terminal window
cargo build
php -d extension=target/debug/libext_php_copilot.dylib tests/acceptance.php