Dec
07
Posted on 07-12-2006
Filed Under (PHP ) by jay

This is another little project that I've been hacking around with which simply uses the Yahoo! Video and YouTube Search Service API's and just for fun phpFlickr to find videos and images.

I've managed to figure out that making a REST request can be broken down to 6 lines of code. Six!

  • The WebService URL
  • 4 Lines of code to set up curl
  • A line that converts the Xml Response into an SimpleXmlObject Array()

Simple Reusable REST Request Method

PHP:
  1. function RESTRequest($base,$params){
  2.    
  3.     $url  = $base.'?'.http_build_query($params);
  4.     $c = curl_init($url);
  5.     curl_setopt($c,CURLOPT_RETURNTRANSFER,true);
  6.     $response = curl_exec($c);
  7.     curl_close($c);
  8.            
  9.     $xml = simplexml_load_string($response);
  10.     return $xml;
  11. }

This will return a SimpleXmlObject than you can then program against.

For example to use this with the Yahoo! Video Search you can invoke RESTRequest like this:

PHP:
  1. $appid = 'SOMEYAHOOID';
  2.     $base = "http://search.yahooapis.com/VideoSearchService/V1/videoSearch";
  3.     $params = array("appid"=>$appid, "query"=>$keyword,"results"=>"20");
  4.    
  5.     $xml = RESTRequest($base,$params);

For Yahoo we need a developer application ID so we set that. Then we create a base URL variable and then we create an array of query string parameters and values. I create the Request URL as 2 variables so at some point they could be placed in configuration files or pulled from a DataBase. Once we are set with the url and parameters we pass them to the RESTRequest($base,$params) and then get back a SimpleXml Object array.

Now that we've got the results back we'll want to display them like this:

PHP:
  1. foreach($xml->Result as $result) { 
  2.         $html .= "<p>\n";      
  3.         $html .= "<a href='".$result->ClickUrl."' title='".$result->DisplayUrl."'>".$result->Title."</a>";
  4.         $html .= "<br>";
  5.         $html .= $result->Summary;
  6.         $html .= "<br>";
  7.         $html .= "<a href='".$result->ClickUrl."' title='".$result->DisplayUrl."'><img src='".$result->Thumbnail->Url."' border='0'></a>";
  8.    
  9.         $html .= "</p>\n";
  10.    
  11. }   
  12. echo $html;

This displays the results as a list with links to the content.

I'm going to skip explaining how Flickr fits into this because I've already covered that here.

Mashing it Up
This isn't really a Mashup in it true form but it's close. So to pull all of these search together I just use a simple search form that uses GET for the Request Method. When the search button is clicked the page submits to it's self and checks to see if the $_GET['txtsearch'] form variable is populated and then I populate some variables to hold the parsed and formatted search results.

PHP:
  1. $keyword = '';
  2. $youtubeResults;
  3. $flickrResults;
  4. $yahooResults;
  5.  
  6. if($_GET['txtsearch'] ) {
  7.     $keyword =$_GET['txtsearch'];
  8.     $youtubeResults = SearchYoutube($keyword);
  9.     $flickrResults = SearchFlickr($f,$keyword)
  10.     $yahooResults =SearchYahoo($keyword);
  11. }

Then to display the search results I just place a php script block in the html where ever I want the results to show up.

CODE:
  1. <?php
  2.     echo $youtubeResults;
  3. ?>

I'm sure you've gotten board with my ramblings at this point so here's a link to a Demo

View the full code

    Read More   
Post a Comment
Name:
Email:
Website:
Comments: