L'applicazione è divisa in due layer (client e server). La parte client sfruttando la libreria jQuery si occupa del layout e di effettuare la chiamata asincrona al codice server, visualizzando le thumbnails dei videos, e al click dell'utente una pop-up permetterà la visione dello stesso (la pop-up utilizza allo scopo swfobject).
La parte server interroga la API e recupera il relativo feed.
Metodi di accesso ad un feed
Codice php:
$doc = new DOMDocument();
$doc->load('http://gdata.youtube.com/feeds/api/videos?vq=funny+dogs&start-index=20&max-results=1');
$group= $doc->getElementsByTagName('group');
var_dump($group->item(0)->nodeValue);
$uri = $doc->documentElement->lookupnamespaceURI('media');
var_dump($uri);
$group= $doc->getElementsByTagNameNS('http://search.yahoo.com/mrss/','group');
var_dump($group->item(0)->nodeValue);
$xp = new domxpath($doc);
$xp->registerNamespace('def','http://www.w3.org/2005/Atom');
$totalResults = $xp->query('/def:feed/def:entry/media:group');
var_dump($totalResults->item(0)->nodeValue);
Come potete vedere dallo snippet possiamo utilizzare diversi metodi per accedere al ns. feed, nella nostra applicazione utiliziamo domxpath.
YouTubeSearchUrl
Codice php:
class YouTubeSearchUrl{
private $start_index= '&start-index=';
private $max_results= '&max-results=';
protected $url= 'http://gdata.youtube.com/feeds/api/videos?';
public function __construct($start,$max){
if(($start==0) || !is_int($start)){
throw new YouTubeException('Invalid start value');
}
if((($max==0) || ($max > 50)) || !is_int($max)){
throw new YouTubeException('Invalid max value');
}
$this->start_index.= $start;
$this->max_results.= $max;
}
protected function url($value){
$this->url.= 'vq='.$value.$this->start_index.$this->max_results;
}
}
Con la class YouTubeSearchUrl non facciamo altro che formare l'url per la query.
YouTubeSearchUrlXml
Codice php:
class YouTubeSearchUrlXml extends YouTubeSearchUrl{
private $xml= null;
private $groups= null;
private $result= array();
public function __construct($start,$max){
parent::__construct($start,$max);
}
public function load($value){
$this->url($value);
$this->xml= new Xml($this->url);
$this->query();
$this->search();
}
private function query(){
$this->xml->xPath();
$this->xml->xp->registerNamespace('def','http://www.w3.org/2005/Atom');
$this->groups= $this->xml->xp->query('/def:feed/def:entry/media:group');
}
public function search(){
for ( $i = 0,$len = $this->groups->length; $i groups->item($i)->getElementsByTagName("title");
$this->result[$i]['title']= $title->item(0)->firstChild->nodeValue;
$description = $this->groups->item($i)->getElementsByTagName("description");
$this->result[$i]['description']= $description->item(0)->firstChild->nodeValue;
$keywords= $this->groups->item($i)->getElementsByTagName("keywords");
$this->result[$i]['keywords']= $keywords->item(0)->firstChild->nodeValue;
$category= $this->groups->item($i)->getElementsByTagName("keywords");
$this->result[$i]['category']= $category->item(0)->firstChild->nodeValue;
$player= $this->groups->item($i)->getElementsByTagName("player");
$this->result[$i]['player']= $player->item(0)->getAttribute('url');
$thumbnail= $this->groups->item($i)->getElementsByTagName("thumbnail");
$this->result[$i]['thumbnail']= $thumbnail->item(0)->getAttribute('url');
$this->result[$i]['width']= $thumbnail->item(0)->getAttribute('width');
$this->result[$i]['height']= $thumbnail->item(0)->getAttribute('height');
}
}
public function result(){
return $this->result;
}
public function json(){
header('Content-type: text/html; charset=utf-8');
echo json_encode($this->result);
}
}
Con YouTubeSearchUrlXml interroghiamo il feed per mezzo di una query Xpath e recuperiamo i valori nell'array result. Il metodo json serve per la risposta al client.
Il codice client non risulta molto complicato per questo motivo e sopratutto per una cronica mancanza di tempo :( non verrà commentato. Invito il lettore a lasciare un commento nel caso di difficoltà.
Puoi scaricare l'intero script qui.