As I suggested in the section that you just read, building a PHP 5 class that has the capacity to work with the Yahoo! Video Search Service is indeed a no-brainer process, since I’m going to use basically the same web searching class that you learned previously. Of course, instead of building a brand new class from scratch, I could derive a new one from the parent, but this would mean getting ahead of myself, and I don’t want to do that. So, let me show you the signature of this video searching class, which has been listed below: // example using Yahoo! Video Search Service - results are displayed in a basic (X)HTML format utilizing a class // define 'VideoSearchService' class class VideoSearchService{ private $requestPath='http://search.yahooapis.com/ private $query='madonna'; private $numResults=10; private $output=''; public function __construct(){} // set search query public function setQuery($query){ if(!$query){ throw new Exception('Invalid search query!'); } $this->query=urlencode($query); } // set number of search results public function setNumResults($numResults){ if(!$numResults||!is_int($numResults)||$numResults>50){ throw new Exception('Invalid number of results!'); } $this->numResults=$numResults; } // perform web search and get search results public function getSearchResults(){ if(!$results=file_get_contents($this->requestPath.'&query= throw new Exception('Error requesting Yahoo! Web service'); } $results=unserialize($results); foreach($results[ResultSet][Result] as $result){ $this->output.='<h2>'.$result[Title].'</h2><p>'.$result } return $this->output; } } Didn’t I tell you before that building a video searching class was going to be a simple process? The brand new “VideoSearchService” class demonstrates what I said. In this case, the class in question implements the Yahoo! Video Search Service. It looks nearly identical to the one I built when working with the conventional search service. Once you grasp the logic implemented by the above video searching class, you should take a look at the following practical example, which shows how to use it in order to display some video entries associated with the search term “Dido”: try{ // create new instance of 'VideoSearchService' class $vs=new VideoSearchService(); // set search query $vs->setQuery('Dido'); // set number of search results $vs->setNumResults(15); // display search results echo $vs->getSearchResults(); } catch(Exception $e){ echo $e->getMessage(); exit(); } // displays the following Dido: Don't Leave Home Music Video by Dido. Song: Don't Leave Home. Label: Arista http://mv.music.yahoo.com/system/admin/tools/getvideo? Dido: White Flag Music Video by Dido. Song: White Flag. Label: Arista Released in http://mv.music.yahoo.com/system/admin/tools/getvideo?vid= Dido: Thankyou Music Video by Dido. Song: Thankyou. Label: Arista Records Released http://mv.music.yahoo.com/system/admin/tools/getvideo?vid=2152196 dido-zmarsu.avi Video: 3469 (2.4 MB format: AVI) Dido z Marsu (8.0 MB) Dido madmix http://www.dido.cz/dido/video/dido-zmarsu.avi dido_nea.mpg Production of Mikado wins National Recognition Current http://www2.potsdam.edu/severtki/dido_nea.mpg dido-madmix.avi 3469 (2.4 MB format: AVI) Dido z Marsu (8.0 MB) Dido madmix http://www.dido.cz/dido/video/dido-madmix.avi dido_sand_in_my_shoes_dsl.wmv http://mfile.akamai.com/5890/wmv/bmgfr.download.akamai.com/ http://mfile.akamai.com/15453/wmv/mtvgermany.download.akamai.com/ dido-papuce.avi Dido madmix (7.6 MB) Dido mánie (7.2 MB) Dido papuče (4.7 MB) http://www.dido.cz/dido/video/dido-papuce.avi karaoke-dido.avi Dido mánie (7.2 MB) Dido papuče (4.7 MB) Karaoke Dido http://www.dido.cz/dido/video/karaoke-dido.avi v_dido_life_for_rent.rm Title: v dido life for rent Noticias Dido_Life for Rent - Noticias http://mfile.akamai.com/6070/rm/bmgsp.download.akamai.com/ dido.wmv Dido - 900k - dur. 1.18 min http://www.romaestate.net/edizione2004/video/dido.wmv dido dido mvmv http://bantan124.castpost.com/dido%20mv.wmv dido_thank_you_2.mov Dido, Here With Me (2)(.mov, 512k) Dido, Thank You (1)(.mov, 392k) http://www.rachelleb.com/images/2004_06_06/ dido_thank_you_1.mov Dido, Here With Me (1)(.mov, 612k) Dido, Here With Me (2) http://www.rachelleb.com/images/2004_06_06/ dido_here_with_me_2.mov Johnathan Rice (.mov, 324k) Dido, Here With Me (1)(.mov, 612k) http://www.rachelleb.com/images/2004_06_06/ As demonstrated above, the “VideoSearchService” class comes in handy for first querying the pertinent Yahoo! Video Service and finally displaying the corresponding results in the browser. Of course, in this case there’s plenty of room to improve the signature of this class, but this will be left as homework for you. Final thoughts In this fifth installment of the series, I showed you how to use an object-oriented approach in order to implement both the traditional and video web search services provided by Yahoo!. As you saw earlier, this process only requires building some easy-to-grasp PHP 5 classes, so you shouldn’t have major problems including them into your existing web applications. In the next tutorial, I’m going to continue demonstrating how to use the object-based paradigm, but this time to implement the Yahoo! Image Search Service. So now that you know what the upcoming article will be about, you don’t have any excuses to miss it!
blog comments powered by Disqus |
|
|
|
|
|
|
|