PHP code example of cheinisch / openrouter-php-client

1. Go to this page and download the library: Download cheinisch/openrouter-php-client 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/ */

    

cheinisch / openrouter-php-client example snippets



heinisch\OpenRouterClient;

$apiKey = getenv('OPENROUTER_API_KEY');

echo OpenRouterClient::OpenRouterChat($apiKey, 'openai/gpt-4o-mini', 'Say only: OK');

echo OpenRouterClient::OpenRouterChat(
    $apiKey,
    'mistralai/mistral-small',
    'Give me one short fun fact about PHP.',
    'https://my-app.com',  // optional – HTTP-Referer
    'My App'               // optional – X-Title
);

$client = new OpenRouterClient($apiKey);

$answer = $client->chat(
    [['role' => 'user', 'content' => 'What is the capital of France?']],
    'openai/gpt-4o-mini'
);

echo $answer; // plain string

$answer = $client->chat(
    [['role' => 'user', 'content' => 'Explain recursion briefly.']],
    'openai/gpt-4o',
    [],   // headers
    [
        'max_completion_tokens' => 256,
        'temperature'           => 0.5,
        'plugins'               => [['id' => 'web']],
    ]
);

$result = $client->chatEx(
    [['role' => 'user', 'content' => 'Summarize todays AI news.']],
    'openai/gpt-4o',
    ['plugins' => [['id' => 'web']]]
);

echo $result['content'];
echo $result['usage']['total_tokens'];

$result = $client->chatEx(
    [['role' => 'user', 'content' => "What's the weather in Berlin?"]],
    'openai/gpt-4o',
    [
        'tools' => [[
            'type'     => 'function',
            'function' => [
                'name'        => 'get_weather',
                'description' => 'Returns the current weather for a city',
                'parameters'  => [
                    'type'       => 'object',
                    'properties' => ['city' => ['type' => 'string']],
                    '

$data = $client->structuredChat(
    [['role' => 'user', 'content' => 'List 3 cities in Germany']],
    [
        'name'   => 'cities',
        'schema' => [
            'type'       => 'object',
            'properties' => [
                'cities' => ['type' => 'array', 'items' => ['type' => 'string']],
            ],
            '

$client->stream(
    [['role' => 'user', 'content' => 'Write a short poem about the ocean.']],
    function(string $delta, bool $done): void {
        if ($done) { echo PHP_EOL; return; }
        echo $delta;
        flush();
    }
);

$result = $client->chatEx(
    [['role' => 'user', 'content' => 'Solve this step by step: 3x + 7 = 22']],
    'anthropic/claude-3-7-sonnet',
    ['reasoning' => ['effort' => 'high']]
);

echo $result['reasoning']; // internal thinking text
echo $result['content'];   // final answer

$result = $client->chatEx(
    [['role' => 'user', 'content' => 'Hello']],
    'openai/gpt-4o',
    [
        'provider' => [
            'sort'            => 'price',
            'data_collection' => 'deny',
        ],
    ]
);

$result = $client->chatEx($messages, 'openai/gpt-4o');
$stats  = $client->getGenerationStats($result['id']);

echo 'Cost: $' . $stats['usage']['cost'];
bash
composer