Home arrow PHP arrow Page 4 - Using Yahoo Web Services to Perform Searches with an Object-Oriented Approach

Building a video searching class with PHP 5 - PHP

If you’re a PHP developer who wants to take the first steps toward building web applications that interact with specific web services, in this article series you’ll find an approachable guide to implementing some of the most useful search services provided by Yahoo! by using a few simple PHP scripts.

TABLE OF CONTENTS:
  1. Using Yahoo Web Services to Perform Searches with an Object-Oriented Approach
  2. Building a web search class with PHP 5
  3. Testing the previous “WebSearchService” class
  4. Building a video searching class with PHP 5
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
February 12, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: