30 May

Motore di Ricerca per il tuo sito (Php + Yahoo).

Ho cercato molte volte un prodotto che potesse fare le ricerche interne al mio sito. Ci sono molti prodotti ma tutti o quasi obbligano and inserire il loro logo o il loro template. Insomma non danno la possibilità di personalizzare al 100% la ricerca sul vostro sito. Così ho ho utilizzato le API di Yahoo e PHP 5(Non penso funzioni con versioni inferiori) e ho modificando il loro script ho creato un motore di ricerca locale per il tuo sito. Qui potete fare delle prove

Sono necessari solo due file common.php e index.php , la richiesta della API key a Yahoo e l’inserimento del vostro sito. E siete pronti ad andare.

Vi inserisco i file qui. (Non linkate direttamente ai file ma alla pagina)

E qui di seguito il codice:

common.php

<?php
// Redevelop of the work from Rasmus Lerdorf
// Please link to mandile.it
// This version uses PHP5’s SimpleXML extension

$appid = ‘YourApi’;
$yoursite = ”;//if empty searches on the web. Example: ‘&site=www.mysite.com’

$service = array(‘web’=>’http://search.yahooapis.com/WebSearchService/V1/webSearch’,
‘image’=>’http://search.yahooapis.com/ImageSearchService/V1/imageSearch’);

header(‘Content-Type: text/html; charset=UTF-8’);
?>
<html>
<head><title>Esempio Yahoo Api Search</title></head>
<body>
<form action=”index.php” method=”GET”>
Search Term: <input type=”text” name=”query” /> <input type=”submit” value=” Go! ” />
<select name=”type”>
<?php foreach($service as $name => $val) {
if(!empty($_REQUEST[‘type’]) && $name == $_REQUEST[‘type’])
echo “<option SELECTED>$name</option>\n”;
else echo “<option>$name</option>\n”;
} ?>
</select>
</form>
<?php

function done() {
echo ‘</body></html>’;
exit;
}

function build_query() {
global $appid, $service;
if(empty($_REQUEST[‘query’]) || !in_array($_REQUEST[‘type’],array_keys($service))) done();

$q = ‘?query=’.rawurlencode(“”.$_REQUEST[‘query’].””);
if(!empty($_REQUEST[‘zip’])) $q.=”&zip=”.$_REQUEST[‘zip’];
if(!empty($_REQUEST[‘start’])) $q.=”&start=”.$_REQUEST[‘start’];
$q .= “&appid=$appid”;
return $q;
}

// Create Previous/Next Page links
function next_prev($res, $start, $last) {
if($start > 1)
echo ‘<a href=”‘.$_SERVER[‘PHP_SELF’].
‘?query=’.rawurlencode($_REQUEST[‘query’]).
‘&type=’.rawurlencode($_REQUEST[‘type’]).
‘&start=’.($start-10).'”><-Previous Page</a> ‘;
if($last < $res[‘totalResultsAvailable’])
echo ‘<a href=”‘.$_SERVER[‘PHP_SELF’].
‘?query=’.rawurlencode($_REQUEST[‘query’]).
‘&type=’.rawurlencode($_REQUEST[‘type’]).
‘&start=’.($last+1).'”>Next Page-></a>’;
}
?>

index.php

<?php
// Redevelop of the work from Rasmus Lerdorf
// Please link to mandile.it
// This version uses PHP5’s SimpleXML extension

require ‘common.php’;
$q=build_query();
// Create a CURL object for later use
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$url = $service[$_REQUEST[‘type’]].$q.$yoursite;
curl_setopt($ch, CURLOPT_URL, $url);
$response = utf8_encode(curl_exec($ch));
//echo $url;
// Then send it to the appropriate service
$xml = simplexml_load_string($response);

// Load up the root element attributes
foreach($xml->attributes() as $name=>$attr) $res[$name]=$attr;
$first = $res[‘firstResultPosition’];
$last = $first + $res[‘totalResultsReturned’]-1;
echo “<p>Matched ${res[totalResultsAvailable]}, showing $first to $last</p>\n”;

print'<script type=”text/javascript”><!–
google_ad_client = “pub-8659254839827892”;
/* 728×15, created 30/05/08 */
google_ad_slot = “8775688669”;
google_ad_width = 728;
google_ad_height = 15;
//–>
</script>
<script type=”text/javascript”
src=”http://pagead2.googlesyndication.com/pagead/show_ads.js”>
</script><br>’;

if(!empty($res[‘ResultSetMapUrl’])) {
echo “<p>Result Set Map: <a href=\”${res[ResultSetMapUrl]}\”>${res[ResultSetMapUrl]}</a></p>\n”;
}

if($_REQUEST[‘type’] == “web”){

foreach($xml as $result) {
//print_r($xml);
echo “<a href=\””.$result->ClickUrl.”\”>”.utf8_encode($result->Title).”</a>”;

echo”<br>”;
echo utf8_encode($result->Summary);
echo”<br>”;
}
}elseif($_REQUEST[‘type’] == “image”){

foreach($xml as $result) {
//print_r($xml);
echo “<a href=\””.$result->ClickUrl.”\”><img src=\””.$result->Thumbnail->Url.”\”></a>”.utf8_encode($result->Summary).””;
echo “File Name:”.utf8_encode($result->Title).””;
echo “Page:”;
echo”<br>”;
echo utf8_encode($result->Summary);
echo”<br>”;
}

};
echo “<br>\n”;
next_prev($res, $first, $last);
done();
?>

Leave a Reply

Your email address will not be published. Required fields are marked *