<?php
declare(strict_types=1);
$apiBase = 'https://hayal.tech/api/v1';
$apiKey = 'YOUR_API_KEY';
function hayalRequest(string $method, string $url, string $apiKey, ?array $payload = null): array
{
$headers = [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json',
];
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_TIMEOUT => 60,
]);
if ($payload !== null) {
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
}
$body = curl_exec($ch);
$status = (int)curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
$error = curl_error($ch);
curl_close($ch);
if ($body === false) {
throw new RuntimeException('HTTP error: ' . $error);
}
$data = json_decode((string)$body, true);
if ($status >= 400) {
$message = $data['error']['message'] ?? $body;
throw new RuntimeException('API error ' . $status . ': ' . $message);
}
return is_array($data) ? $data : [];
}
// 1. Проверить баланс и режим.
$me = hayalRequest('GET', $apiBase . '/me', $apiKey);
print_r($me);
// 2. Получить доступные модели.
$models = hayalRequest('GET', $apiBase . '/models', $apiKey);
print_r($models);
// 3. Запустить генерацию на генерацию картинки.
$job = hayalRequest('POST', $apiBase . '/jobs', $apiKey, [
'kind' => 'image',
'model' => 'sfw-image-1',
'prompt' => 'Квадратная рекламная картинка товара, студийный свет, чистый фон',
'options' => [
'ratio' => '1:1',
'quality' => '1k',
'count' => 1,
],
'reference_urls' => [
// 'https://example.com/reference.jpg',
],
]);
print_r($job);
// 4. Проверить статус задачи.
$jobId = $job['data']['id'] ?? null;
if ($jobId) {
$status = hayalRequest('GET', $apiBase . '/jobs/' . $jobId, $apiKey);
print_r($status);
}