PHPackages                             patryk-samulewicz/open-php-router - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. patryk-samulewicz/open-php-router

ActiveLibrary[API Development](/categories/api)

patryk-samulewicz/open-php-router
=================================

A modern PHP package for OpenRouter API integration

v1.0.5(10mo ago)012MITPHPPHP ^8.2

Since Jul 12Pushed 10mo agoCompare

[ Source](https://github.com/Patryk-Samulewicz/open-php-router)[ Packagist](https://packagist.org/packages/patryk-samulewicz/open-php-router)[ RSS](/packages/patryk-samulewicz-open-php-router/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (3)Versions (7)Used By (0)

Open PHP Router
===============

[](#open-php-router)

Nowoczesna paczka PHP do integracji z OpenRouter API. Umożliwia łatwe pobieranie listy modeli, wysyłanie zapytań chat oraz sprawdzanie kosztów generowania.

Instalacja
----------

[](#instalacja)

```
composer require open-php-router/open-php-router
```

Konfiguracja
------------

[](#konfiguracja)

```
use OpenPhpRouter\OpenRouterClient;

$client = new OpenRouterClient([
    'api_key' => 'your_openrouter_api_key',
    'timeout' => 30, // opcjonalnie
    'referer' => 'https://your-app.com', // opcjonalnie
    'title' => 'your-app-name', // opcjonalnie
]);
```

Pobieranie listy modeli
-----------------------

[](#pobieranie-listy-modeli)

```
use OpenPhpRouter\OpenRouterClient;

$client = new OpenRouterClient(['api_key' => 'your_api_key']);
$models = $client->listModels();

foreach ($models as $model) {
    echo sprintf(
        "%s (%s) - Prompt: $%.4f/1K, Completion: $%.4f/1K\n",
        $model->getName(),
        $model->getProvider(),
        $model->getPromptPricePer1K() ?? 0,
        $model->getCompletionPricePer1K() ?? 0
    );
}
```

Wysyłanie zapytania chat
------------------------

[](#wysyłanie-zapytania-chat)

```
use OpenPhpRouter\OpenRouterClient;
use OpenPhpRouter\DTO\ChatData;
use OpenPhpRouter\DTO\MessageData;
use OpenPhpRouter\Enum\RoleType;

$client = new OpenRouterClient(['api_key' => 'your_api_key']);

$chatData = new ChatData(
    messages: [
        MessageData::createUserMessage('Hello, who are you?'),
    ],
    model: 'mistralai/mistral-7b-instruct:free',
    maxTokens: 100,
    temperature: 0.7,
);

$response = $client->chat($chatData);
echo $response->getContent();
```

Sprawdzanie kosztów generowania
-------------------------------

[](#sprawdzanie-kosztów-generowania)

### Metoda 1: Pobieranie kosztów przez ID generowania

[](#metoda-1-pobieranie-kosztów-przez-id-generowania)

```
// Po wykonaniu zapytania chat
$response = $client->chat($chatData);
$generationId = $response->getId();

// Pobierz szczegółowe informacje o kosztach
$costs = $client->getRequestCosts($generationId);

echo sprintf(
    "Prompt tokens: %d (%.4f USD)\n",
    $costs->getPromptTokens(),
    $costs->getPromptCost()
);

echo sprintf(
    "Completion tokens: %d (%.4f USD)\n",
    $costs->getCompletionTokens(),
    $costs->getCompletionCost()
);

echo sprintf(
    "Total cost: %.4f USD\n",
    $costs->getTotalCost()
);

echo sprintf(
    "Cost per 1K prompt tokens: %.4f USD\n",
    $costs->getPromptCostPer1K()
);

echo sprintf(
    "Cost per 1K completion tokens: %.4f USD\n",
    $costs->getCompletionCostPer1K()
);
```

### Metoda 2: Pobieranie kosztów bezpośrednio z odpowiedzi

[](#metoda-2-pobieranie-kosztów-bezpośrednio-z-odpowiedzi)

```
$response = $client->chat($chatData);

// Pobierz koszty bezpośrednio z odpowiedzi
$costs = $client->getChatRequestCosts($response);

echo sprintf(
    "ID generowania: %s\n",
    $costs->getId()
);

echo sprintf(
    "Model: %s\n",
    $costs->getModel()
);

echo sprintf(
    "Total cost: %.4f %s\n",
    $costs->getTotalCost(),
    $costs->getCurrency()
);
```

Przykład z system message
-------------------------

[](#przykład-z-system-message)

```
$chatData = new ChatData(
    messages: [
        MessageData::createSystemMessage('You are a helpful assistant.'),
        MessageData::createUserMessage('What is the capital of Poland?'),
    ],
    model: 'mistralai/mistral-7b-instruct:free',
    maxTokens: 50,
);

$response = $client->chat($chatData);
echo $response->getContent();
```

Dostępne metody
---------------

[](#dostępne-metody)

### OpenRouterClient

[](#openrouterclient)

- `listModels()` - Pobiera listę dostępnych modeli z cenami
- `chat(ChatData $chatData)` - Wysyła zapytanie chat i zwraca odpowiedź
- `getGeneration(string $generationId)` - Pobiera surowe dane o generowaniu
- `getRequestCosts(string $generationId)` - Pobiera koszty dla konkretnego requesta
- `getChatRequestCosts(ChatResponseData $response)` - Pobiera koszty bezpośrednio z odpowiedzi chat

### GenerationCostData

[](#generationcostdata)

- `getId()` - ID generowania
- `getModel()` - Nazwa modelu
- `getPromptTokens()` - Liczba tokenów prompt
- `getCompletionTokens()` - Liczba tokenów completion
- `getTotalTokens()` - Całkowita liczba tokenów
- `getPromptCost()` - Koszt prompt
- `getCompletionCost()` - Koszt completion
- `getTotalCost()` - Całkowity koszt
- `getCurrency()` - Waluta (domyślnie USD)
- `getPromptCostPer1K()` - Koszt na 1K tokenów prompt
- `getCompletionCostPer1K()` - Koszt na 1K tokenów completion

Obsługa błędów
--------------

[](#obsługa-błędów)

```
use OpenPhpRouter\Exception\OpenRouterException;

try {
    $models = $client->listModels();
} catch (OpenRouterException $e) {
    echo 'Błąd OpenRouter: ' . $e->getMessage();
}
```

Wymagania
---------

[](#wymagania)

- PHP 8.2+
- cURL extension (zazwyczaj wbudowane w PHP)

Licencja
--------

[](#licencja)

MIT License

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance55

Moderate activity, may be stable

Popularity6

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

6

Last Release

302d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/739c38e08ded0fb45956eecdb42adc8cea0e15946dc3c21b41eccea03ef5a12f?d=identicon)[Patryk-Samulewicz](/maintainers/Patryk-Samulewicz)

---

Top Contributors

[![Patryk-Samulewicz](https://avatars.githubusercontent.com/u/97521999?v=4)](https://github.com/Patryk-Samulewicz "Patryk-Samulewicz (8 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/patryk-samulewicz-open-php-router/health.svg)

```
[![Health](https://phpackages.com/badges/patryk-samulewicz-open-php-router/health.svg)](https://phpackages.com/packages/patryk-samulewicz-open-php-router)
```

###  Alternatives

[stripe/stripe-php

Stripe PHP Library

4.0k143.3M480](/packages/stripe-stripe-php)[twilio/sdk

A PHP wrapper for Twilio's API

1.6k92.9M272](/packages/twilio-sdk)[knplabs/github-api

GitHub API v3 client

2.2k15.8M187](/packages/knplabs-github-api)[facebook/php-business-sdk

PHP SDK for Facebook Business

90121.9M34](/packages/facebook-php-business-sdk)[meilisearch/meilisearch-php

PHP wrapper for the Meilisearch API

73813.7M114](/packages/meilisearch-meilisearch-php)[google/gax

Google API Core for PHP

263103.1M454](/packages/google-gax)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
