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

Building a web search 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.

  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



According to the concepts deployed in the introduction, my intention here is simply to demonstrate how to use an object-based approach to implement a specific Yahoo! Web Search Service. Based upon this objective, first I’m going to show you how to create a compact, highly modular PHP 5 class, which will be responsible first for querying the Yahoo! Web Search Service, and then returning the potential results neatly formatted to client code.

That being said, pay close attention to the signature of the following class, which in this case is tasked with using the aforementioned Yahoo! web service:

// example using Yahoo! Web Search Service - results are displayed in a basic (X)HTML format utilizing a class

// define ' WebSearchService' class

class WebSearchService{

private $requestPath='http://api.search.yahoo.com/WebSearchService/V1/webSearch?

private $query='madonna';

private $numResults=10;

private $output='';

public function __construct(){}

// set search query

public function setQuery($query){


throw new Exception('Invalid search query!');




// set number of search results

public function setNumResults($numResults){


throw new Exception('Invalid number of results!');




// perform web search and get search results

public function getSearchResults(){


throw new Exception('Error requesting Yahoo! Web service');



foreach($results[ResultSet][Result] as $result){



return $this->output;



If you study the signature that corresponds to the above “WebSearchService” class that I just built in detail, then you’ll quickly grasp the way it works. Basically, the class in question exposes three primary methods aside from the pertinent constructor. The first two, called “setQuery()” and “setNumResults()” respectively, become very convenient for specifying (in that order) the search string that will be used to query the service and the number of results that should be returned to the browser. So far, nothing unexpected.

Obviously, the workhorse of the class is its “getSearchResults()” method, which implements the necessary logic to retrieve the corresponding search results, which are finally formatted and returned to client code.

As you can see, building a class that has the capacity for implementing the Yahoo! Web Search Service is not only simple, but also fun! Undoubtedly, the signature of the previous class can be largely improved, but it truly does a decent job using the mentioned web service.

Okay, now that you learned how to build a PHP 5 class capable of implementing the Yahoo! Web Search Service with minor hassles, I believe that it's time to move on and see how it functions in the context of a real-word example.

Thus, go ahead and read the next section to see how the previous “WebSearchService” class can be put to work with minor efforts.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: