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? // 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].' } 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.... http://madonnalicious.com/downloads/madonna_2004_montage.wmv madonna_earlscourt_190804.wmv http://www.madonnalicious.com/downloads/madonna_gelredome http://www.madonnalicious.com/downloads/madonna_earlscourt_190804.wmv madonna.al.et.mpg Summary: Watch Madonna on Entertainment Tonight : http://steveswartz.com/madonna.al/madonna.al.et.mpg madonna_jonathanross_ruperteverett.wmv And Save Target As Friday Night With Jonathan Ross - http://www.madonnalicious.com/downloads/madonna madonna_gos_240804.wmv has just aired on local TV note the sound sync maybe http://madonnalicious.com/downloads/madonna_gos_240804.wmv madonna_earlscourt_dtm_190804.wmv http://www.madonnalicious.com/downloads/madonna http://www.madonnalicious.com/downloads/madonna_ The Androids: Do It With Madonna Music Video by The Androids. Song: Do It With Madonna. 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 - 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 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 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!
blog comments powered by Disqus |
|
|
|
|
|
|
|