As you may have noticed, in many of my articles on PHP web development I like to reintroduce previous hands-on examples of how to perform a certain task with this great server-side scripting language. And this one won’t be an exception to the rule. Before I provide you with concrete code samples regarding the implementation of Yahoo!’s video and image web search services with PHP 5, I'm going to list the complete source code of the example that you learned in the previous tutorial. As you'll probably recall, it showed you how to invoke the traditional Yahoo! Search Service. So having said this, please take some time to look at the following code sample, which queries the web service in question with the search term “Madonna” and at the end, displays the returned results by using a few basic (X)HTML tags: try{ $request='http://api.search.yahoo.com/WebSearchService/V1/webSearch? // trigger the http request if(!$results=file_get_contents($request)){ throw new Exception('Error requesting Yahoo! Web service'); } $results=unserialize($results); foreach($results[ResultSet][Result] as $result){ echo '<h2>'.$result[Title].'</h2><p>'.$result[Summary].'</p><a href="'.$result } } catch(Exception $e){ echo $e->getMessage(); exit(); } // displays the following: Madonna Official site of pop diva Madonna, with news, music, media, and fan club. http://www.madonna.com/ MySpace.com - Madonna - Pop / Rock - www.myspace.com/madonna Madonna MySpace page with news, blog, music downloads, desktops, wallpapers, and more. http://www.myspace.com/madonna If you study closely the above practical example, you’ll see that implementing the Yahoo! Web Search Service with PHP 5 is a relatively simple process that can be tackled even if you’re a beginner at PHP programming. As shown by the previous code sample, first the script queries the mentioned service by using the corresponding application ID provided by Yahoo! along with the respective search term and the “output=php” option. Then the potential results are returned in the form of a serialized array. Pretty understandable, right? Next, those results are properly unserialized and then displayed on the browser via a common “foreach” PHP statement. In addition, it’s worthwhile to highlight that the web service always sends the output wrapped in a “ResultSet” array element, which can be easily parsed by utilizing some regular PHP array handling functions. So far, so good. At this stage you've hopefully recalled the basics for implementing the Yahoo! Web Search Service with PHP 5. Thus it’s an excellent moment to see how to apply the same business logic that you learned previously to use the respective image and video search services. Therefore, considering that learning how to incorporate these two useful Yahoo! web services into your PHP applications can potentially be interesting, go ahead and read the next section. I’ll be there waiting for you.
blog comments powered by Disqus |
|
|
|
|
|
|
|