Created
April 6, 2010 12:56
-
-
Save timjb/357560 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include 'assets/snippets/bosssearch.php'; | |
return bossSearch(); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function bossSearch() { | |
// Bester Spaghetticode! | |
// settings | |
$appid = ''; | |
$sites = ''; // CSV | |
$resultsperpage = 10; | |
$debug = false; | |
if(isset($_GET['page']) && preg_match("/[0-9]+/", $_GET['page']) && $_GET['page'] >= 1) { | |
$page = (int) $_GET['page']; | |
} | |
else { | |
$page = 1; | |
} | |
$start = ($page - 1) * $resultsperpage; | |
if(isset($_GET['search']) && $_GET['search'] !== '') { | |
$q = trim($_GET['search']); | |
} | |
else { | |
return '<p>Kein Suchterm!</p>'; | |
} | |
$url = 'http://boss.yahooapis.com/ysearch/web/v1/'.urlencode($q). | |
'?appid='.$appid. | |
'&sites='.urlencode($sites). | |
'&start='.$start. | |
'&count='.$resultsperpage; | |
$data = file_get_contents($url); | |
$json = json_decode($data, true); | |
$json = $json['ysearchresponse']; | |
// output | |
$html = ''; | |
// debug infos | |
if($debug) { | |
$html .= '<h3>API call</h3> <p><a href='.$url.'>'.$url.'</a></p>'; | |
$html .= '<h3>Response</h3> <p>'.$data.'</p>'; | |
$html .= '<h3>Output</h3>'; | |
} | |
// search results | |
$q = htmlentities($q); // security | |
if($json['totalhits'] == '0') { | |
$html .= '<p>Ihre Suche nach "'.$q.'" lieferte keine Ergebnisse.</p>'; | |
} | |
else { | |
if($json['totalhits'] == '1') { | |
$html .= '<p>Ihre Suche nach "'.$q.'" lieferte ein Ergebnis:</p>'; | |
} | |
else { | |
$html .= '<p>Ergebnisse '.($start + 1).' – '.($start + $json['count']).' von '.$json['totalhits'].' für Ihre Suche nach "'.$q.'":</p>'; | |
} | |
$html .= '<ul>'; | |
foreach($json['resultset_web'] as $result) { | |
$html .= '<li>'; | |
$html .= '<h3><a href="'.$result['clickurl'].'">'.$result['title'].'</a></h3>'; | |
$html .= '<p>'.$result['abstract'].'</p>'; | |
$html .= '</li>'; | |
} | |
$html .= '</ul>'; | |
// page nav | |
if($json['totalhits'] > $json['count']) { | |
$html .= '<div class="pagination clearfix">'; | |
$html .= '<div class="page prevpage">'; | |
if($page > 1) { | |
$html .= '<a href="search.html?search='.$q.'&page='.($page - 1).'">« Vorherige Seite</a>'; | |
} | |
$html .= '</div><div class="page thispage">'; | |
$html .= 'Seite '.$page; | |
$html .= '</div><div class="page nextpage">'; | |
if($json['totalhits'] > $start + $json['count']) { | |
$html .= '<a href="search.html?search='.$q.'&page='.($page + 1).'">Nächste Seite »</a>'; | |
} | |
$html .= '</div>'; | |
$html .= '</div>'; | |
} | |
} | |
return $html; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment