50 lines
1.4 KiB
PHP
50 lines
1.4 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
|
|
function fetchBingResults($query) {
|
|
$url = "https://www.bing.com/search?q=" . urlencode($query) . "&count=10";
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36');
|
|
|
|
$html = curl_exec($ch);
|
|
curl_close($ch);
|
|
|
|
if (!$html) return [];
|
|
|
|
$dom = new DOMDocument();
|
|
@$dom->loadHTML($html);
|
|
|
|
$results = [];
|
|
$blocks = $dom->getElementsByTagName('div')->item(0)->getElementsByTagName('div');
|
|
|
|
foreach ($blocks as $block) {
|
|
$title = $block->getElementsByTagName('h2')->item(0)->textContent;
|
|
$link = $block->getElementsByTagName('a')->item(0)->getAttribute('href');
|
|
$description = $block->getElementsByTagName('p')->item(0)->textContent;
|
|
|
|
$results[] = [
|
|
'title' => trim($title),
|
|
'link' => trim($link),
|
|
'description' => trim($description),
|
|
'icon' => null // 网页版Bing不显示图标
|
|
];
|
|
}
|
|
|
|
return $results;
|
|
}
|
|
|
|
$searchTerm = isset($_GET['p']) ? $_GET['p'] : 'Kentucky';
|
|
$results = fetchBingResults($searchTerm);
|
|
|
|
$response = [
|
|
'results' => $results,
|
|
'total' => count($results),
|
|
'max_page' => 1
|
|
];
|
|
|
|
echo json_encode($response);
|
|
?>
|