PHP nie ma klienta AI – zapytania poprzez curl
Klucz API generujemy w Claude Console i przechowujemy zaszyfrowany w bazie danych
ALTER TABLE brands ADD anthropic_api_key_enc text DEFAULT NULL COMMENT "anthropic_api_key_enc";
.env – klucz szyfrujący (32 bajty = 256 bitów):
# php -r "echo base64_encode(random_bytes(32));"
config/app_local.php
// Szyfrowanie kluczy API i innych wrażliwych danych 'Encryption' => [ 'key' => 'meDX61dddsfasrfLMUwTxdfsdadfad=', ],
Serwis szyfrujący
// src/Service/EncryptionService.php namespace App\Service; use Cake\Core\Configure; use RuntimeException; class EncryptionService { private string $key; private string $cipher = 'aes-256-gcm'; public function __construct() { $raw = base64_decode(Configure::read('Encryption.key', '')); if (strlen($raw) !== 32) { throw new RuntimeException('APP_ENCRYPTION_KEY musi mieć 32 bajty (base64).'); } $this->key = $raw; } // Szyfruje tekst np. API KEY i koduje w Base64 public function encrypt(string $plaintext): string { $ivLen = openssl_cipher_iv_length($this->cipher); $iv = random_bytes($ivLen); $tag = ''; $encrypted = openssl_encrypt($plaintext, $this->cipher, $this->key, 0, $iv, $tag); // Zapisujemy IV + tag + ciphertext razem (base64) return base64_encode($iv . $tag . $encrypted); } // Odszyfrowuje zawartość np. zaszyfrowany API KEY w formie Base64 public function decrypt(string $stored): string { $decoded = base64_decode($stored); $ivLen = openssl_cipher_iv_length($this->cipher); $tagLen = 16; // GCM tag zawsze 16 bajtów $iv = substr($decoded, 0, $ivLen); $tag = substr($decoded, $ivLen, $tagLen); $encrypted = substr($decoded, $ivLen + $tagLen); $plaintext = openssl_decrypt($encrypted, $this->cipher, $this->key, 0, $iv, $tag); if ($plaintext === false) { throw new RuntimeException('Odszyfrowanie nie powiodło się — zły klucz lub dane uszkodzone.'); } return $plaintext; } }
src/Service/AnthropicService.php
namespace App\Service; use RuntimeException; use App\Model\Entity\Brand; use Cake\Core\Configure; use App\Service\EncryptionService; class AnthropicService { private string $apiKey; private string $apiUrl = 'https://api.anthropic.com/v1/messages'; private string $model = 'claude-sonnet-4-20250514'; private int $maxTokens = 1024; private int $maxHistory = 20; // max par wiadomości, potem przycinamy public function __construct(Brand $brand) { // Klucz API marki (zaszyfrowany z bazy danych) if (!empty($brand->anthropic_api_key_enc)) { $enc = new EncryptionService(); $this->apiKey = $enc->decrypt($brand->anthropic_api_key_enc); } // Klucz API globalny (jeśli marka nie ma własnego) else { $this->apiKey = Configure::read('Anthropic.apiKey', ''); } if (empty($this->apiKey)) { throw new RuntimeException("Brak klucza Anthropic dla marki {$brand->name}."); } } // construct /** * Wysyła wiadomość i zwraca odpowiedź. * Historia jest przekazywana z zewnątrz (sesja lub baza). */ public function chat(string $userMessage, array $history = [], string $systemPrompt = ''): array { // Dodajemy nową wiadomość użytkownika do historii $messages = $history; $messages[] = ['role' => 'user', 'content' => $userMessage]; // Przycinamy historię żeby nie przekroczyć limitu tokenów if (count($messages) > $this->maxHistory * 2) { $messages = array_slice($messages, -($this->maxHistory * 2)); } $payload = [ 'model' => $this->model, 'max_tokens' => $this->maxTokens, 'messages' => $messages, ]; if ($systemPrompt !== '') { $payload['system'] = $systemPrompt; } $ch = curl_init($this->apiUrl); curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_HTTPHEADER => [ 'x-api-key: ' . $this->apiKey, 'anthropic-version: 2023-06-01', 'content-type: application/json', ], CURLOPT_POSTFIELDS => json_encode($payload), ]); $body = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); $response = json_decode($body, true); if ($httpCode !== 200) { $error = $response['error']['message'] ?? 'Nieznany błąd API'; throw new RuntimeException("Błąd Anthropic API ({$httpCode}): {$error}"); } $assistantMessage = $response['content'][0]['text']; // Zwracamy odpowiedź + zaktualizowaną historię $updatedHistory = $messages; $updatedHistory[] = ['role' => 'assistant', 'content' => $assistantMessage]; return [ 'message' => $assistantMessage, 'history' => $updatedHistory, 'usage' => $response['usage'] ?? [], ]; } // chat } // class AnthropicService
Użycie w kontrolerze
// src/Controller/ChatController.php
namespace App\Controller;
use App\Service\AnthropicService;
class ChatController extends AppController
{
public function index(): void
{
$anthropic = new AnthropicService();
$answer = $anthropic->ask(
'Jakie są zalety CakePHP 5?',
'Jesteś ekspertem PHP. Odpowiadaj zwięźle po polsku.'
);
$this->set(compact('answer'));
}
}