Files
bookmarkly2/api/pexels.php
T
2025-09-28 16:05:53 +02:00

43 lines
1.2 KiB
PHP

<?php
// API endpoint for searching Pexels
// This should be called via AJAX from the settings page.
require_once __DIR__ . '/../include/auth.php'; // Reuse auth check
header('Content-Type: application/json');
// Get API Key from settings
$stmt = $pdo->prepare("SELECT value FROM settings WHERE key = ?");
$stmt->execute(['pexels_api_key']);
$api_key = $stmt->fetchColumn();
if (empty($api_key)) {
echo json_encode(['error' => 'Pexels API key is not set in settings.']);
exit;
}
$query = trim($_GET['query'] ?? '');
if (empty($query)) {
echo json_encode(['error' => 'Search query is required.']);
exit;
}
$url = "https://api.pexels.com/v1/search?query=" . urlencode($query) . "&per_page=15";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: ' . $api_key
]);
$result = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_code !== 200) {
echo json_encode(['error' => 'Failed to fetch images from Pexels.', 'details' => json_decode($result)]);
exit;
}
echo $result;