Spaces:
Running
Running
| ```php | |
| header("Content-Type: application/json"); | |
| header("Access-Control-Allow-Origin: *"); | |
| header("Access-Control-Allow-Methods: GET, POST, OPTIONS"); | |
| header("Access-Control-Allow-Headers: Content-Type"); | |
| // Database configuration | |
| $servername = "localhost"; | |
| $username = "your_username"; | |
| $password = "your_password"; | |
| $dbname = "tokigunesparkevleri_db"; | |
| // Create connection | |
| $conn = new mysqli($servername, $username, $password, $dbname); | |
| // Check connection | |
| if ($conn->connect_error) { | |
| die(json_encode(["error" => "Connection failed: " . $conn->connect_error])); | |
| } | |
| // Handle different API endpoints | |
| $request = $_SERVER['REQUEST_URI']; | |
| $method = $_SERVER['REQUEST_METHOD']; | |
| // Contact form submission | |
| if ($method === 'POST' && strpos($request, '/api/contact') !== false) { | |
| $data = json_decode(file_get_contents('php://input'), true); | |
| $name = $data['name'] ?? ''; | |
| $email = $data['email'] ?? ''; | |
| $phone = $data['phone'] ?? ''; | |
| $message = $data['message'] ?? ''; | |
| if (empty($name) || empty($email) || empty($message)) { | |
| http_response_code(400); | |
| echo json_encode(["error" => "Missing required fields"]); | |
| exit; | |
| } | |
| $stmt = $conn->prepare("INSERT INTO contacts (name, email, phone, message, created_at) VALUES (?, ?, ?, ?, NOW())"); | |
| $stmt->bind_param("ssss", $name, $email, $phone, $message); | |
| if ($stmt->execute()) { | |
| echo json_encode(["success" => "Message sent successfully"]); | |
| } else { | |
| http_response_code(500); | |
| echo json_encode(["error" => "Failed to send message"]); | |
| } | |
| $stmt->close(); | |
| exit; | |
| } | |
| // Get Instagram posts | |
| if ($method === 'GET' && strpos($request, '/api/instagram') !== false) { | |
| $result = $conn->query("SELECT * FROM instagram_posts ORDER BY post_date DESC LIMIT 6"); | |
| $posts = []; | |
| while ($row = $result->fetch_assoc()) { | |
| $posts[] = [ | |
| 'id' => $row['id'], | |
| 'image_url' => $row['image_url'], | |
| 'author' => $row['author'], | |
| 'author_image' => $row['author_image'], | |
| 'content' => $row['content'], | |
| 'likes' => $row['likes'], | |
| 'posted_at' => $row['post_date'] | |
| ]; | |
| } | |
| echo json_encode($posts); | |
| exit; | |
| } | |
| // Newsletter subscription | |
| if ($method === 'POST' && strpos($request, '/api/newsletter') !== false) { | |
| $data = json_decode(file_get_contents('php://input'), true); | |
| $email = $data['email'] ?? ''; | |
| if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) { | |
| http_response_code(400); | |
| echo json_encode(["error" => "Invalid email address"]); | |
| exit; | |
| } | |
| $stmt = $conn->prepare("INSERT INTO newsletter_subscribers (email, subscribed_at) VALUES (?, NOW())"); | |
| $stmt->bind_param("s", $email); | |
| if ($stmt->execute()) { | |
| echo json_encode(["success" => "Subscribed successfully"]); | |
| } else { | |
| http_response_code(500); | |
| echo json_encode(["error" => "Subscription failed"]); | |
| } | |
| $stmt->close(); | |
| exit; | |
| } | |
| // Default response for unknown endpoints | |
| http_response_code(404); | |
| echo json_encode(["error" => "Endpoint not found"]); | |
| $conn->close(); | |
| ``` |