Backend Controller: gemini_proxy.php
This script sends your SEO data to Gemini and returns the structured JSON analysis.
<?php // 1. Setup Configuration $apiKey = 'YOUR_GEMINI_API_KEY'; $apiUrl = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=' . $apiKey; // 2. The System Prompt (From our previous step) $systemPrompt = "You are a Senior SEO Specialist. Return analysis in JSON format only..."; // 3. User Content (Usually from a $_POST request) $userContent = "<h1>Welcome to my site</h1><p>We sell the best widgets in California.</p>"; // 4. Prepare the JSON Payload $data = [ "contents" => [[ "parts" => [ ["text" => $systemPrompt], ["text" => "Analyze this HTML: " . $userContent] ] ]] ]; // 5. Execute cURL Request $ch = curl_init($apiUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); $response = curl_exec($ch); curl_close($ch); // 6. Output Result $result = json_decode($response, true); print_r($result['candidates'][0]['content']['parts'][0]['text']); ?>
Implementation Tips for Your Team:
- Security: Never hardcode your API key in a public GitHub repo. Use an
.envfile or a separate config file that is.gitignore‘d. - Error Handling: Add a check for
if(curl_errno($ch))to ensure your VPS can actually reach Google’s servers. - Model Choice: I used Gemini 1.5 Flash in the URL above because it is faster and cheaper for SEO audits, which is perfect for an MVP.
Leave a Reply