PHP: JSON: AJAX: Youtube Video Aramaları
|
#7817 |
|
Kaan
User is:
Posts: 2772
Meslek: Serbest Meslek
Age: 41
|
Youtube video arama tekniğini (gdata json api) kullanarak basit bir ajax arama sistemi yapalım.
Uygulamada 2 farklı php dosyası oluşturduk. Ana dosya (index.php) ve ajax postun cevaplandığı arama (videosearch.php) dosyası uygulamanın çalışması için yeterlidir. Basit bir arama formu ile aranan kelime, jquery post methodu ile, videosearch.php dosyasına aktarılır ve youtube gdata apisine gönderilir. index.php kodlarını inceleyelim: HTML4STRICT: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Youtube JSON Video Arama</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script> <script type="text/javascript"> <?php if(isset($_GET['q'])){ ?> $.ajax({ type: 'POST', url: 'videosearch.php', data: 'key=<?=$_GET['q']?>', success:function(ajaxCevap) { $('#sonuclar').html(ajaxCevap); } }); <?php } ?> </script> </head> <body> <h1>Demo</h1> <hr /> <form method="get"><strong>Aranacak kelime:</strong> <input type="text" name="q" value="<?=$_GET['q']?>"/> <input type="submit" value="Ara" /></form> <p>&amp;amp;nbsp;</p> <div id="sonuclar" align="center"> <?php if(isset($_GET['q'])){ ?> <img src="fav-loader.gif" align="middle" /> <br /> <strong>Sonuçlar yükleniyor</strong> <?php }else{ ?> <strong>Videolar için arama kutucuğunu kullanınız.</strong> <?php } ?> </div> <hr /> <div style="clear:both;"></div> <br /> PHP: JSON: AJAX: Youtube Video Aramaları</strong> yazısı için hazırlanmıştır. </body> </html> ndex.php ile alınan arama kelimesi, kodlarda görebileceğiniz gibi jquery yardımıyla videosearch.php dosyasına post edilir ve gelen cevap sonuclar div katmanının içine aktarılır. videosearch.php kodları: PHP: <style> html{font-family:Verdana, Geneva, sans-serif; font-size:11px;} a{color:#009; text-decorationne;} a img{borderne;} </style> <?php $key=$_POST['key']; function VideoSearch($searchTerms) { $googleBaseUrl = "http://gdata.youtube.com/feeds/api/videos?q=".$searchTerms."&amp;amp;format=5&amp;amp;start-index=1&amp;amp;max-results=20&amp;amp;v=2&amp;amp;alt=jsonc"; /* CURL ile bağlanılacak youtube gdata bağlantısı*/ $googleFullUrl = $googleBaseUrl; $curlObject = curl_init(); curl_setopt($curlObject,CURLOPT_URL,$googleFullUrl); curl_setopt($curlObject,CURLOPT_RETURNTRANSFER,true); curl_setopt($curlObject,CURLOPT_HEADER,false); curl_setopt($curlObject,CURLOPT_REFERER,"http://www.site.com/"); $returnGoogleSearch = curl_exec($curlObject); curl_close($curlObject); $returnGoogleSearch = json_decode($returnGoogleSearch,true); /*json formatında gelen cevap decode edilir*/ return $returnGoogleSearch['data']['items']; } $setSearchTerms = urlencode($key); $searchResults = VideoSearch($setSearchTerms); if(sizeof($searchResults)>0){ foreach($searchResults as $result){ ?> <div style="width:150px; float:left; padding:3px; margin:5px; height:130px; overflow:hidden;"> <a href="http://www.youtube.com/watch?v=<?=$result['id']?>" title="<?=$result['title']?>"> <img src="<?=$result['thumbnail']['sqDefault']?>" width="120" alt="<?=$result['title']?>" title="<?=$result['title']?>"/> </a> <p align="center"> <a href="http://www.youtube.com/watch?v=<?=$result['id']?>" title="<?=$result['title']?>"> <?=$result['title']?> </a> </p> </div> <?php } } ?> |
Emlak, Oto Galeri, Rent A Car, Şiir, Edebiyat Script Siparişlerinizi Verebilirsiniz. Detaylar İçin: kaan@ntka.org Seditio 170 İndir Capte Müzik İndir Seditio Toolbar İndir |