Logo classes.scriptsphp.org PHP

go to nav bar

PHP5 Source codes

Recherche sur le web

Ce formulaire vous permet d'effectuer des recherches sur le web (ou sur ce site) en utilisant les moteurs de recherche Yahoo ou Google.
Ce «meta-moteur» est basé sur les API :

Rechercher uniquement sur ce site

Source PHP de la classe Yahoo

<?php 

class YahooWebSearch  {

    private 
$APIURL 'http://api.search.yahoo.com/WebSearchService/V1/webSearch';
    private 
$LicenceKey '';
    static 
$Instance false;
    
    private function 
__construct() {
        
$this->LicenceKey YAHOO_LICENCE_KEY;
    }
    
    static function 
GetInstance() {
        if(empty(
self::$Instance)) {
            
self::$Instance = new YahooWebSearch();
        }
        return 
self::$Instance;
    }

    public function 
search($query$start=0$results=10$similar_ok=false$adult_ok=false$lang='fr') {
        
// ----------------------------------
        // paramètres non pris en compte :
        
$type 'all';
        
$format 'all';
        
$country '';
        
// ----------------------------------
        
        
$s['appid'] = $this->LicenceKey;
        
$s['query'] = urlencode(utf8_encode($query));
        
        
// ------------------------------------------------------------
        // Non utilisé
        
if($type == 'any' || $type == 'phrase' || $type == 'all') {
            
$s['type'] = $type;
        }
        
$formats_ok = array('html''msword''pdf''rss''ppt''xls''txt');
        if(
in_array($format$formats_ok)) {
            
$s['format'] = $format;
        }
        
// ------------------------------------------------------------
        
if($results || $results <= 50) {
            
$s['results'] = $results;
        }
        
// Google démarre à 0, Yahoo à 1
        
if($start >= 0) {
            
$s['start'] = $start+1;
        }
        if(
$adult_ok == 1) {
            
$s['adult_ok'] = 1;
        }
        if(
$similar_ok == 1) {
            
$s['similar_ok'] = 1;
        }
        
$s['language'] = $lang;

        foreach(
$s as $param => $value) {
            
$search[] = $param.'='.$value;
        }

        
$xml = @simplexml_load_file($this->APIURL.'?'implode('&'$search));
        if(!
is_object($xml)) {        
            throw new 
Exception('REST error : unable to load XML file');
            return 
false;
        }
        return 
$this->toArray($xml$query);
    }

    private function 
toArray($XMLObject$search) {
        
$ret['Search'] = $search;
        foreach(
$XMLObject->attributes() as $key => $val) {
            if(
$key == 'totalResultsAvailable') {
                
$ret['TotalResults'] = (int)$val;
            } elseif(
$key == 'firstResultPosition') {
                
$ret['Start'] = (int)$val;
            }
        }
        
$i 1;
        foreach(
$XMLObject->Result as $val) {
            
$ret['Results'][$i = ++$i+1] = array(
            
'Title' => (string)utf8_decode($val->Title),
            
'URL' => (string)$val->Url,
            
'Summary' => (string)utf8_decode($val->Summary)
            );
        }
        return 
$ret;
    }

}
?>

Merci de ne pas suivre ce lien emails.

0.1481s | «»
PHP powered