Defining Some Custom PHP Functions with Yahoo Web Services - Using Yahoo! Video Search Service (
Page 3 of 4 )
As you might guess, creating a custom PHP function that uses the Yahoo! Video Search Service is very similar to working with the traditional searches that you saw in the previous section. But to dissipate any possible doubts that you might have regarding this topic, below I included the definition of a brand new PHP function, which implements the helpful Yahoo! Video Search Service.
Given that, this function, called “getVideoSearchResults()” (yes, my creativity with names continues to blow me away) looks like this:
// example using Yahoo! Video Search Service - results are displayed in a basic (X)HTML format utilizing an user-defined PHP function
// define 'getVideoSearchResults()' function
function getVideoSearchResults($query='madonna',$numResults=10){
if(!$query){
throw new Exception('Invalid search query!');
}
if(!$numResults||!is_int($numResults)){
throw new Exception('Invalid number of results!');
}
$query=urlencode($query);
$request='http://search.yahooapis.com/VideoSearchService/V1/videoSearch?
appid=Your-AP-ID&query='.$query.'&results='.$numResults.'&output=php';
// trigger the http request
if(!$results=file_get_contents($request)){
throw new Exception('Error requesting Yahoo! Web service');
}
$results=unserialize($results);
$output='';
foreach($results[ResultSet][Result] as $result){
$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 $output;
}
Evidently, the definition of the above custom PHP function is very similar to the one I used to work with conventional web searches. Of course, in this specific case, the function will fetch the corresponding search results from a different URL, and format them utilizing different elements of the pertinent output array.
In addition, below I coded a simple script that first calls the previous “getVideoSearchResults()” PHP function, and then displays on the browser the results produced by the respective video search process. Have a look at the following code sample, please:
try{
// call 'getVideoSearchResults()' function
echo getVideoSearchResults();
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
// displays the following search results
madonna_2004_montage.wmv
, interviews, news footage and tour clips from New York and Paris....
here's hoping 2005 is just as busy! Right Click And Save Target As
Madonna's 2004 Movie Montage (WMV) - 7:44 - 21.8Mb
Noticias madonna - Noticias madonna - Noticias madonna -
http://madonnalicious.com/downloads/madonna_2004_montage.wmv
madonna_earlscourt_190804.wmv
http://www.madonnalicious.com/downloads/madonna_gelredome
_090904.wmv madonna 9sep 2004
http://www.madonnalicious.com/downloads/madonna_earlscourt
_190804.wmv
http://www.madonnalicious.com/downloads/madonna_earlscourt
_dtm_190804.wmv
http://www.madonnalicious.com/downloads/madonna_earlscourt_190804.wmv
madonna.al.et.mpg
Summary: Watch Madonna on Entertainment Tonight :
Madonna discusses American Life with ET .mpg Noticias madonna -
Noticias madonna - Noticias madonna -
http://steveswartz.com/madonna.al/madonna.al.et.mpg
madonna_jonathanross_ruperteverett.wmv
And Save Target As Friday Night With Jonathan Ross -
Rupert Everett On Madonna (WMV) - 1:25 - 2.45Mb JR
Noticias madonna - Noticias madonna - Noticias madonna -
http://www.madonnalicious.com/downloads/madonna
_jonathanross_ruperteverett.wmv
madonna_gos_240804.wmv
has just aired on local TV note the sound sync maybe
slightly off in the video file Right Click And Save Target As
Madonnas Visit To Great Ormond Street London Tonight
Report WMV 128 985Mb Noticias madonna -
Noticias madonna - Noticias madonna -
http://madonnalicious.com/downloads/madonna_gos_240804.wmv
madonna_earlscourt_dtm_190804.wmv
http://www.madonnalicious.com/downloads/madonna
_earlscourt_190804.wmv
http://www.madonnalicious.com/downloads/madonna
_earlscourt_dtm_190804.wmv
http://www.madonnalicious.com/downloads/madonna
_gelredome_music_090904.wmv
http://www.madonnalicious.com/downloads/madonna_
earlscourt_dtm_190804.wmv
The Androids: Do It With Madonna
Music Video by The Androids. Song: Do It With Madonna.
Label: Universal Records Released in 2003.
http://mv.music.yahoo.com/system/admin/tools/getvideo?vid=2156622
Britney Spears: Me Against The Music - Featuring Madonna
Music Video by Britney Spears. Song: Me Against The Music -
Featuring Madonna. Label: Zomba Recording Corp. Released in 2003.
http://mv.music.yahoo.com/system/admin/tools/getvideo?vid=2158463
madonna defends adoption decision
pop star madonna insists she's done nothing wrong in adopting a
baby boy from malawi, saying she has acted within the law.
http://cosmos.bcst.yahoo.com/up/yaustralia/news?ch=248153&cl=997154
Madonna's adoption woes continue
Madonna's baby adoption has taken yet another turn. The father
of 13-month-old David claims he never intended his son to be
adopted by the pop diva, only for her to raise him.
http://cosmos.bcst.yahoo.com/up/yaustralia/news?ch=248153&cl=1022403
As you can see, building a custom PHP 5 function that implements the Yahoo! Video Search Service is only a matter of triggering an HTTP request to the correct URL and then parsing the results using a simple “foreach” loop. Period.
And speaking of working with some of the most popular web services offered by Yahoo!, in the last section of this tutorial I’m going to teach you how to build yet another brand new PHP function, which this time will be tasked with working with its helpful image search service.
Naturally, in order to learn the details about how this function will be created, you’ll have to click on the link below and keep reading. We’re almost finished!