İnternet kullanıcıları, arama motorlarında belirli terimler aradıklarında, çoğu zaman bu terimleri yazarken otomatik tamamlayıcı önerilerle karşılaşırlar. Bu öneriler, arama motorlarının en popüler ve yaygın kullanılan sorguları baz alarak kullanıcılara arama önerileri sunmasına yardımcı olur. Bu yazımızda, PHP kullanarak Google, Bing, YouTube ve Yahoo arama motorlarından anlık arama önerilerini nasıl çekebileceğinizi göstereceğiz.
Google, kullanıcıların arama sorgularına göre önerilerde bulunur. Google’ın bu önerileri sağlamak için sunduğu API'yi kullanarak PHP ile arama önerilerini alabilirsiniz. Google önerilerini almak için aşağıdaki gibi bir fonksiyon kullanabilirsiniz:
function getGoogleSuggestions($query) {
$url = "http://suggestqueries.google.com/complete/search?client=firefox&ds=yt&q=" . urlencode($query);
// cURL ile istek gönderme
$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 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36");
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'cURL Error: ' . curl_error($ch);
return [];
}
curl_close($ch);
// JSON formatındaki yanıtı ayrıştırma
$suggestions = json_decode($response, true);
// Öneri terimlerini döndürme
return isset($suggestions[1]) ? $suggestions[1] : [];
}
Bu fonksiyon, belirtilen sorguya karşılık gelen Google arama önerilerini döndürecektir.
Yahoo, kendi öneri sistemini API aracılığıyla sunmaktadır. Yahoo'nun önerilerini çekmek için aşağıdaki fonksiyonu kullanabilirsiniz:
function getYahooSuggestions($query) {
$url = "https://search.yahoo.com/sugg/ff?output=firefox&command=" . urlencode($query);
$response = file_get_contents($url);
// JSONP veriyi JSON formatına dönüştürme
$response = trim($response, '()');
$suggestions = json_decode($response, true);
$result = [];
if (isset($suggestions['gossip']['results'])) {
foreach ($suggestions['gossip']['results'] as $suggestion) {
$result[] = $suggestion['key'];
}
}
return $result;
}
Yahoo'dan dönen JSONP formatını işleyip geçerli bir JSON yapısına dönüştürmek için trim()
fonksiyonuyla baştaki ve sondaki parantezler temizlenir ve ardından veriler işlenir.
YouTube, Google’a ait bir platform olduğu için, Google’ın arama önerileri API'siyle YouTube önerilerini de alabilirsiniz. Bu şekilde YouTube için öneri çekerken de aynı API kullanılır.
function getYouTubeSuggestions($query) {
$url = "http://suggestqueries.google.com/complete/search?client=firefox&ds=yt&q=" . urlencode($query) . "&hl=en";
// cURL ile istek gönderme
$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 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36");
$response = curl_exec($ch);
if(curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
return [];
}
curl_close($ch);
// JSON formatındaki yanıtı ayrıştırma
$suggestions = json_decode($response, true);
// İlk index, önerilen terimleri içerir
return isset($suggestions[1]) ? $suggestions[1] : [];
}
Bing de benzer şekilde arama önerilerini sunar. Bing önerilerini çekmek için aşağıdaki fonksiyonu kullanabilirsiniz:
function getBingSearchSuggestions($query) {
$url = 'http://api.bing.com/osjson.aspx?Query=' . urlencode($query);
// cURL ile istek gönderme
$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 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36');
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$response = curl_exec($ch);
if(curl_errno($ch)) {
echo 'cURL Error: ' . curl_error($ch);
return [];
}
curl_close($ch);
// JSON verisini çözümleme
$data = json_decode($response, true);
if (isset($data[1])) {
return $data[1]; // Öneri terimleri genellikle 2. dizide bulunur
}
return [];
}
$query = "php"; // Örnek arama terimi
$googleSuggestions = getGoogleSuggestions($query);
$BingSuggestions = getBingSearchSuggestions($query);
$youtubeSuggestions = getYouTubeSuggestions($query);
$yahooSuggestions = getYahooSuggestions($query);
echo "Google => ";
print_r($googleSuggestions);
echo "Bing => ";
print_r($googleSuggestions);
echo "YouTube => ";
print_r($youtubeSuggestions);
echo "Yahoo => ";
print_r($yahooSuggestions);
Bu makale sayesinde PHP ile Google, Bing, YouTube ve Yahoo arama motorlarından anlık arama önerilerini alabilir ve bu verileri kullanıcılarınıza sunabilirsiniz. Bu tür öneri sistemleri, web uygulamalarında arama deneyimini iyileştirmek ve kullanıcı etkileşimini artırmak için çok faydalıdır.
Yorumlar (0)