body { font-family: monospace; background: #000; color: #0f0; padding: 20px; } h2, h3 { color: #0ff; } pre { background: #111; padding: 15px; border-left: 3px solid #0f0; overflow-x: auto; } .error { color: #f00; } .success { color: #0f0; } .warning { color: #ff0; } img { background: #fff; padding: 10px; } "; echo "

🔍 Gemini API Diagnostic Tool

"; // Test 1: Check API key echo "

Test 1: API Key Check

"; $apiKey = GEMINI_API_KEY; if (empty($apiKey)) { echo "

❌ No API key configured!

"; exit; } else { echo "

✅ API Key: " . substr($apiKey, 0, 10) . "..." . substr($apiKey, -6) . "

"; } // Test 2: Try to list available models echo "

Test 2: Check Available Models

"; $listUrl = 'https://generativelanguage.googleapis.com/v1beta/models'; $ch = curl_init($listUrl . '?key=' . $apiKey); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $modelsResponse = curl_exec($ch); $modelsData = json_decode($modelsResponse, true); curl_close($ch); if (isset($modelsData['models'])) { echo "

✅ API connection successful!

"; echo "

Available models with IMAGE generation:

"; } else { echo "

❌ Could not fetch models. Response:

"; echo "
" . json_encode($modelsData, JSON_PRETTY_PRINT) . "
"; } // Test 3: Try the image generation with the current model echo "

Test 3: Image Generation Test

"; $model = 'gemini-3-pro-image-preview'; $url = "https://generativelanguage.googleapis.com/v1beta/models/{$model}:generateContent"; $payload = [ 'contents' => [ [ 'parts' => [ ['text' => 'A simple red circle on white background'] ] ] ], 'generationConfig' => [ 'responseModalities' => ['IMAGE'], 'imageConfig' => [ 'aspectRatio' => '1:1', 'imageSize' => '1K' ] ] ]; echo "

Model: $model

"; echo "

URL: $url

"; echo "
Request Payload (click to expand)"; echo "
" . json_encode($payload, JSON_PRETTY_PRINT) . "
"; echo "
"; $ch = curl_init($url); curl_setopt_array($ch, [ CURLOPT_HTTPHEADER => [ 'Content-Type: application/json', 'x-goog-api-key: ' . $apiKey ], CURLOPT_POST => true, CURLOPT_POSTFIELDS => json_encode($payload), CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => true, CURLOPT_TIMEOUT => 120 ]); echo "

⏳ Sending request...

"; $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $curlError = curl_error($ch); curl_close($ch); echo "

HTTP Status Code: "; if ($httpCode == 200) { echo "$httpCode ✅

"; } else { echo "$httpCode ❌

"; } if ($curlError) { echo "

cURL Error: $curlError

"; } $responseData = json_decode($response, true); echo "
Full Response (click to expand)"; echo "
" . json_encode($responseData, JSON_PRETTY_PRINT) . "
"; echo "
"; // Check for specific error messages if (isset($responseData['error'])) { echo "

❌ API Error Details:

"; echo "
" . json_encode($responseData['error'], JSON_PRETTY_PRINT) . "
"; if (isset($responseData['error']['message'])) { $errorMsg = $responseData['error']['message']; echo "

Error Message: $errorMsg

"; // Provide helpful suggestions if (stripos($errorMsg, 'not found') !== false || stripos($errorMsg, 'models/') !== false) { echo "

💡 Suggestion: The model name might be incorrect or deprecated.

"; } elseif (stripos($errorMsg, 'quota') !== false || stripos($errorMsg, 'limit') !== false) { echo "

💡 Suggestion: You may have exceeded your API quota.

"; } elseif (stripos($errorMsg, 'internal') !== false) { echo "

💡 Suggestion: This might be a temporary API issue or the model is having problems.

"; } } } // Check for image in response if (isset($responseData['candidates'][0]['content']['parts'][0]['inlineData']['data'])) { echo "

✅ SUCCESS! Image Generated!

"; $imageData = $responseData['candidates'][0]['content']['parts'][0]['inlineData']['data']; $mimeType = $responseData['candidates'][0]['content']['parts'][0]['inlineData']['mimeType']; echo "

MIME Type: $mimeType

"; echo "

Data length: " . strlen($imageData) . " characters

"; echo "

Generated Image:

"; echo ""; } else { echo "

❌ No Image Data Found

"; } echo "

Debug completed at " . date('Y-m-d H:i:s') . "

"; ?>