Building a Complete Web Searching Class with Yahoo Web Services and PHP 5 - Defining an image searching class in PHP 5 (Page 2 of 4 )
I need to show you something before I teach you how to use Inheritance to build some child classes that will concretely implement each of the Yahoo! Web Search Services discussed in the previous articles of the series. Below you will see the corresponding definition of a simple class that is tasked with consuming the Yahoo Image Search Service, which was left undefined in the last tutorial.
Here's the signature of this brand new PHP class, which is responsible for performing image searches using the corresponding Yahoo API. Take a look at it, please:
// example using Yahoo! Image Search Service - results are displayed in a basic (X)HTML format utilizing a class
// define 'ImageSearchService' class
class ImageSearchService{
private $requestPath='http://search.yahooapis.com/
ImageSearchService/V1/imageSearch?appid=Your-AP-ID';
private $query='madonna';
private $numResults=10;
private $output='';
public function __construct(){}
// set search query
public function setQuery($query){
if(!$query){
throw new Exception('Invalid search query!');
}
$this->query=urlencode($query);
}
// set number of search results
public function setNumResults($numResults){
if(!$numResults||!is_int($numResults)||$numResults>50){
throw new Exception('Invalid number of results!');
}
$this->numResults=$numResults;
}
// perform web search and get search results
public function getSearchResults(){
if(!$results=file_get_contents($this->requestPath.'&query='
.$this->query.'&results='.$this->numResults.'&output=php')){
throw new Exception('Error requesting Yahoo! Web service');
}
$results=unserialize($results);
foreach($results[ResultSet][Result] as $result){
$this->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 $this->output;
}
}
As you can see, the definition of the above "ImageSearchService" class looks very similar to the ones that I defined in the previous tutorial, which were aimed at working with the respective Yahoo traditional and image web searches. In this case, I built this class so it can perform image searches very easily, which is certainly a process that's clearly demonstrated by the following hands-on example:
try{
// create new instance of 'ImageSearchService' class
$is=new ImageSearchService();
// set search query
$is->setQuery('Dido');
// set number of search results
$is->setNumResults(10);
// display search results
echo $is->getSearchResults();
}
catch(Exception $e){
echo $e->getMessage();
exit();
}
// displays the following
dido_270.jpe
http://fanwebdido.wz.cz/fotogalerie/dido_270.jpe
Dido-Life_For_Rent-Frontal.jpg
Enrique Iglesias - 7 50 cent-Get rich or die.. Dido - Life for rent
Safri duo - 3.0 (new)
http://www.coveralia.com/audio/d/Dido-Life_For_Rent-
Frontal.jpg
Dido_kontje.jpg
http://xs45.xs.to/pics/05365/Dido_kontje.jpg
dido_1.jpg
http://peoples.ru/art/music/pop/dido/dido_1.jpg
Dido.jpg
http://biografie.leonardo.it/img/bio/d/Dido.jpg
dido.jpg
Pop chanteuse Dido entered London's Guildhall School of
Music at age six , and had mastered the piano, the violin
and the recorder by the time she was 10. After touring with a
http://www.grabow.biz/images/dido.jpg
dido.jpg
dezembro 25, 2005 Parabans 1954 - Annie Lennox , cantora
1971 - Dido , cantora Publicado por nelourenco Ă s
http://atuleirus.weblog.com.pt/arquivo/dido.jpg
dido128.jpg
Dido: 'I camped out the night before Wham! The Final'.
Photo: Pete Millson
http://image.guardian.co.uk/sys-images/Guardian/Pix/arts/
2003/10/02/dido128.jpg
Dido2654.jpg
http://www.sirensofsong.com/Dido/images/Dido2654.jpg
INT-Dido.jpg
Dido Born Florian Cloud de Bounevialle Armstrong, on the
25th of December 1971, in England, Dido is the sister of
Faithless mastermind and leading UK dance music producer,
Rollo. She
http://www.annecarlini.com/images/exclusive/interviews/INT-Dido.jpg
Undoubtedly, consuming the Yahoo! Image Search Service by using the class that I built earlier is not only a no-brainer process that can be tackled with minor effort, but a fun experience! Okay, I have to admit that it actually isn't so fun for some, but keep in mind that I have to market this class to you somehow.
Well, I assume at this time that the previous example should give you a clear idea of how to build a PHP class that utilizes the Yahoo! Image Search Service in a useful fashion. So, what's the next step?
In accordance with the concepts that I deployed in the beginning, in the next section I'm going to teach you how to use Inheritance to create different subclasses. Each of them will be focused on consuming a specific Yahoo! Web service. In this manner, we will develop a more efficient object-oriented approach that will work with the services in question.
To find out how these subclasses will be built, please jump ahead and read the next few lines.
Next: Using Inheritance to work with different Yahoo! Web Services >>
More PHP Articles
More By Alejandro Gervasio