Created
June 8, 2026 03:25
-
-
Save myriaglot/05918c298a2ff77549a8224154e6d49b to your computer and use it in GitHub Desktop.
claude-anki-quiz.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env php | |
| <?php | |
| // ANSI escape codes for terminal formatting | |
| $reset = "\033[0m"; | |
| $green = "\033[32m"; | |
| $red = "\033[31m"; | |
| $yellow = "\033[33m"; | |
| $cyan = "\033[36m"; | |
| $bold = "\033[1m"; | |
| // Clear the screen at start | |
| echo "\033[2J\033[;H"; | |
| // 1. Resolve API key | |
| $apiKey = getenv('OPENAI_API_KEY'); | |
| if (!$apiKey) { | |
| echo "{$yellow}{$bold}No OPENAI_API_KEY found in your environment variables.{$reset}\n"; | |
| echo "Please paste your OpenAI API Key: "; | |
| $apiKey = trim(fgets(STDIN)); | |
| if (empty($apiKey)) { | |
| echo "{$red}API key is required. Exiting.{$reset}\n"; | |
| exit(1); | |
| } | |
| } | |
| echo "\033[2J\033[;H"; | |
| echo "{$cyan}{$bold}================================================={$reset}\n"; | |
| echo "π‘ CONNECTING TO LLM GENERATION LAYER (PHP CLI)\n"; | |
| echo "{$cyan}{$bold}================================================={$reset}\n"; | |
| echo "Generating fresh Claude API questions...\n\n"; | |
| // 2. Prepare Payload for OpenAI/pi.dev Endpoint | |
| $url = 'https://api.openai.com/v1/chat/completions'; | |
| $payload = [ | |
| 'model' => 'gpt-4o-mini', | |
| 'messages' => [ | |
| [ | |
| 'role' => 'system', | |
| 'content' => 'You are an expert technical interviewer testing developers on the Anthropic Claude API, prompt caching, tokens, and agentic loops. Generate exactly 3 advanced questions. Return ONLY a JSON object containing an array named "questions". Each item must have: "question", "key_phrase" (the concept to remember), and "explanation".' | |
| ], | |
| [ | |
| 'role' => 'user', | |
| 'content' => 'Generate a new distinct batch of advanced quiz items.' | |
| ] | |
| ], | |
| 'response_format' => ['type' => 'json_object'] | |
| ]; | |
| // 3. Native cURL Request (No dependencies needed) | |
| $ch = curl_init($url); | |
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
| curl_setopt($ch, CURLOPT_POST, true); | |
| curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload)); | |
| curl_setopt($ch, CURLOPT_HTTPHEADER, [ | |
| 'Content-Type: application/json', | |
| 'Authorization: Bearer ' . $apiKey | |
| ]); | |
| $response = curl_exec($ch); | |
| if (curl_errno($ch)) { | |
| echo "{$red}Curl Error: " . curl_error($ch) . "{$reset}\n"; | |
| exit(1); | |
| } | |
| curl_close($ch); | |
| // 4. Parse response | |
| $data = json_decode($response, true); | |
| $questionsBlock = json_decode($data['choices'][0]['message']['content'] ?? '{}', true); | |
| $questions = $questionsBlock['questions'] ?? []; | |
| if (empty($questions)) { | |
| echo "{$red}Failed to parse questions from LLM payload. Try again.{$reset}\n"; | |
| exit(1); | |
| } | |
| // 5. Run the Quiz Loop | |
| echo "\033[2J\033[;H"; | |
| echo "{$green}{$bold}β¨ Questions Loaded! Starting Practice Run...{$reset}\n\n"; | |
| foreach ($questions as $index => $item) { | |
| $currentNum = $index + 1; | |
| $total = count($questions); | |
| echo "{$bold}Question {$currentNum}/{$total}:{$reset}\n"; | |
| echo "{$cyan}{$item['question']}{$reset}\n\n"; | |
| echo "{$yellow}Type your answer or memory phrase:{$reset} "; | |
| $userAttempt = trim(fgets(STDIN)); | |
| echo "\n-------------------------------------------------\n"; | |
| echo "{$bold}Key Phrase to Remember:{$reset} {$green}{$bold}{$item['key_phrase']}{$reset}\n"; | |
| echo "{$bold}Explanation:{$reset} {$item['explanation']}\n"; | |
| echo "-------------------------------------------------\n\n"; | |
| echo "Did your answer align with the core phrase? (y/n): "; | |
| $evaluation = trim(fgets(STDIN)); | |
| if (strtolower(substr($evaluation, 0, 1)) === 'y') { | |
| echo "π {$green}Nice job! Concept retained.{$reset}\n"; | |
| } else { | |
| echo "π‘ {$yellow}No worries. Focus on the core key phrase.{$reset}\n"; | |
| } | |
| echo "\nPress Enter to proceed..."; | |
| fgets(STDIN); | |
| echo "\033[2J\033[;H"; // Clear screen for next question | |
| } | |
| echo "{$bold}{$green}================================================={$reset}\n"; | |
| echo "π Practice session closed. Run again for a new batch!\n"; | |
| echo "{$bold}{$green}================================================={$reset}\n\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment