PHP code example of nandung / copilot-proxy

1. Go to this page and download the library: Download nandung/copilot-proxy 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/ */

    

nandung / copilot-proxy example snippets




use Nandung\CopilotProxy\CopilotProxy;

// Get device code
$auth = CopilotProxy::auth();
$deviceCode = $auth->getDeviceCode();

echo "Go to: {$deviceCode->verificationUri}\n";
echo "Enter code: {$deviceCode->userCode}\n";

// Wait for user to authorize (polls automatically)
$githubToken = $auth->pollAccessToken($deviceCode, function($attempt, $max) {
    echo "Waiting for authorization... ({$attempt}/{$max})\n";
});

// Save token for later use
file_put_contents('github_token.txt', $githubToken);
echo "Token saved!\n";



use Nandung\CopilotProxy\CopilotProxy;

$proxy = new CopilotProxy(file_get_contents('github_token.txt'));

// Simple question
$answer = $proxy->ask('What is the capital of France?');
echo $answer; // Paris

// Get user info
$user = $proxy->getUser();
echo "Logged in as: {$user['login']}\n";

// Non-streaming
$response = $proxy->chat([
    'model' => 'gpt-4.1',
    'messages' => [
        ['role' => 'system', 'content' => 'You are a helpful assistant.'],
        ['role' => 'user', 'content' => 'Hello!']
    ],
    'max_tokens' => 1000,
    'temperature' => 0.7,
]);

echo $response->getContent();
echo "Tokens used: {$response->usage['total_tokens']}\n";

// Streaming
foreach ($proxy->chatStream([
    'model' => 'gpt-4.1',
    'messages' => [['role' => 'user', 'content' => 'Tell me a story']]
]) as $chunk) {
    echo $chunk->getDeltaContent();
    flush();
}

$models = $proxy->models();

foreach ($models as $model) {
    echo "- {$model->id} ({$model->vendor})\n";
    echo "  Max tokens: {$model->getMaxOutputTokens()}\n";
}

$embedding = $proxy->embeddings('Hello world', 'text-embedding-3-small');
$vector = $embedding->getFirstEmbedding();
echo "Vector dimensions: " . count($vector) . "\n";

$response = $proxy->chat([
    'model' => 'gpt-4.1',
    'messages' => [
        ['role' => 'user', 'content' => 'What is the weather in Tokyo?']
    ],
    'tools' => [
        [
            'type' => 'function',
            'function' => [
                'name' => 'get_weather',
                'description' => 'Get weather for a location',
                'parameters' => [
                    'type' => 'object',
                    'properties' => [
                        'location' => ['type' => 'string']
                    ],
                    '

// Individual (default)
$proxy = new CopilotProxy($token, 'individual');

// Business
$proxy = new CopilotProxy($token, 'business');

// Enterprise
$proxy = new CopilotProxy($token, 'enterprise');

use Nandung\CopilotProxy\Exceptions\CopilotException;

try {
    $response = $proxy->chat([...]);
} catch (CopilotException $e) {
    echo "Error: {$e->getMessage()}\n";
    echo "Status: {$e->getStatusCode()}\n";
    
    if ($errorData = $e->getErrorData()) {
        print_r($errorData);
    }
}

// In AppServiceProvider

use Nandung\CopilotProxy\CopilotProxy;

public function register()
{
    $this->app->singleton(CopilotProxy::class, function () {
        return new CopilotProxy(config('services.copilot.token'));
    });
}

// Usage in controller
public function index(CopilotProxy $proxy)
{
    return $proxy->ask('Hello!');
}