Examples Reference
Summary
Section titled “Summary”This page documents the bundled scripts and common usage recipes. Run examples with the debug extension loaded.
Requirements
Section titled “Requirements”| Requirement | Description |
|---|---|
| Extension build | Run cargo build before executing examples. |
| Extension path | macOS uses target/debug/libext_php_copilot.dylib; Linux uses target/debug/libext_php_copilot.so; Windows uses a .dll. |
| Authentication | Set GITHUB_COPILOT_TOKEN or use explicit CLI-user configuration for local development. |
Bundled Scripts
Section titled “Bundled Scripts”| Script | Purpose |
|---|---|
examples/basic.php | Direct native client/session flow. |
examples/models.php | Model listing. |
examples/streaming.php | Streaming event polling. |
examples/generic_app.php | Recommended wrapper flow for generic PHP apps. |
Running Examples
Section titled “Running Examples”Synopsis
Section titled “Synopsis”cargo buildphp -d extension=target/debug/libext_php_copilot.dylib examples/basic.phpphp -d extension=target/debug/libext_php_copilot.dylib examples/models.phpphp -d extension=target/debug/libext_php_copilot.dylib examples/streaming.phpphp -d extension=target/debug/libext_php_copilot.dylib examples/generic_app.phpReplace the extension path with the Linux .so or Windows .dll path when running on another platform.
Recipe: Generic Wrapper Flow
Section titled “Recipe: Generic Wrapper Flow”Description
Section titled “Description”Use ExtPhpCopilot\Copilot when integrating Copilot into an application. This flow keeps auth, JSON handling, session lifecycle, and cleanup in one wrapper.
Example
Section titled “Example”<?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();}Recipe: Direct Native Flow
Section titled “Recipe: Direct Native Flow”Description
Section titled “Description”Use Copilot\Client and Copilot\Session directly when you need raw JSON options or streaming/session control.
Example
Section titled “Example”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();Recipe: Model Listing
Section titled “Recipe: Model Listing”Description
Section titled “Description”Call Copilot\Client::modelsJson() to inspect available model metadata.
Example
Section titled “Example”$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();Recipe: Streaming Events
Section titled “Recipe: Streaming Events”Description
Section titled “Description”Create a streaming session, send a prompt with immediate delivery, and poll for events.
Example
Section titled “Example”$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;}Recipe: Live Acceptance Test
Section titled “Recipe: Live Acceptance Test”Description
Section titled “Description”The acceptance test loads .env, verifies authentication, sends one prompt, and stores local Copilot CLI state under var/copilot-acceptance.
Environment
Section titled “Environment”GITHUB_COPILOT_TOKEN=your_token_hereCommand
Section titled “Command”cargo buildphp -d extension=target/debug/libext_php_copilot.dylib tests/acceptance.phpSee Also
Section titled “See Also”📦 Source: soderlind/ext-php-copilot · Edit on GitHub