35082-vm/api/search.php
Flatlogic Bot b113664b26 A-1
2025-10-21 11:59:17 +00:00

28 lines
702 B
PHP

<?php
header('Content-Type: application/json');
$query = isset($_GET['q']) ? trim($_GET['q']) : '';
if (empty($query)) {
echo json_encode(['error' => 'Query parameter is missing.']);
exit;
}
$url = 'https://api.duckduckgo.com/?q=' . urlencode($query) . '&format=json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'ArtickleSearch/1.0');
$output = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_code !== 200) {
http_response_code(500);
echo json_encode(['error' => 'Failed to fetch data from the external API.']);
exit;
}
echo $output;