Using Yahoo Web Services to Perform Searches with an Object-Oriented Approach - Building a video searching class with PHP 5
(Page 4 of 4 )
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/
VideoSearchService/V1/videoSearch?appid=Your-AP-ID';
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=
'.$this->query.'&results='.$this->numResults.'&output=php')){
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
[Summary].'</p><p><img src="'.$result[Thumbnail][Url].
'" width="'.$result[Thumbnail][Width].'" height="'.$result
[Thumbnail][Height].'" /></p><a href="'.$result[Url].'">'.
$result[Url].'</a>';
}
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
Released in 2004.
http://mv.music.yahoo.com/system/admin/tools/getvideo?
vid=2165529
Dido: White Flag
Music Video by Dido. Song: White Flag. Label: Arista Released in
2003.
http://mv.music.yahoo.com/system/admin/tools/getvideo?vid=
2158072
Dido: Thankyou
Music Video by Dido. Song: Thankyou. Label: Arista Records Released
in 2001.
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
(7.6 MB)
http://www.dido.cz/dido/video/dido-zmarsu.avi
dido_nea.mpg
Production of Mikado wins National Recognition Current
Production Schedule 2004 clip of Dido and Aeneas Audition
information for Orpheus in the Underworld
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
(7.6 MB) Dido mánie (7.2 MB)
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/
5890/windows_media/video/v_amel_bent_ma_philosophie_high.wmv
Dido
http://mfile.akamai.com/15453/wmv/mtvgermany.download.akamai.com/
15453/wmv/ondemand/stars/dido/dido_sand_in_my_shoes_dsl.wmv
Gwen stefani
http://mfile.akamai.com/15453/wmv/mtvgermany.download.akamai.com/
15453/wmv/ondemand/stars/dido/dido_sand_in_my_shoes_dsl.wmv
dido-papuce.avi
Dido madmix (7.6 MB) Dido mánie (7.2 MB) Dido papuče (4.7 MB)
Karaoke Dido (4.8 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
(4.8 MB) Audio:
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
Dido_Life for Rent - Noticias Dido_Life for Rent -
http://mfile.akamai.com/6070/rm/bmgsp.download.akamai.com/
6070/video/v_dido_life_for_rent.rm
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)
Dido, Thank You (2)(.mov, 520k) Dido, Thank You (3)(.mov, 488k)
http://www.rachelleb.com/images/2004_06_06/
dido_thank_you_2.mov
dido_thank_you_1.mov
Dido, Here With Me (1)(.mov, 612k) Dido, Here With Me (2)
(.mov, 512k) Dido, Thank You (1)(.mov, 392k) Dido, Thank You
(2)(.mov, 520k)
http://www.rachelleb.com/images/2004_06_06/
dido_thank_you_1.mov
dido_here_with_me_2.mov
Johnathan Rice (.mov, 324k) Dido, Here With Me (1)(.mov, 612k)
Dido, Here With Me (2)(.mov, 512k) Dido, Thank You (1)(.mov, 392k)
http://www.rachelleb.com/images/2004_06_06/
dido_here_with_me_2.mov
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!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |