Defining Some Custom PHP Functions with Yahoo Web Services

Welcome to the fourth installment of the series “Using Yahoo! Web Services with PHP 5.” Made up of six approachable tutorials, this series provides you with the right pointers to start using these useful web services within the context of your PHP 5-driven applications. It complements the corresponding theoretical concepts with a decent variety of hands-on examples.

As you may know, the incorporation of popular web services, such as image and video searches, into existing web applications can be an efficient approach to expanding their functionality and making them more attractive to end users. Based upon this concept, Yahoo! has made a set of powerful APIs available for PHP developers, which, among other useful things, will let you couple its most relevant web services to your own PHP applications without needing to deal directly with data served in WSDL (Web Service Description Language).

However, as with everything in Life, learning how to put those Yahoo! services to work for you takes some time. If you read this series of tutorials, it will hopefully turn the overall educational process into a painless experience.

Now that you’ve read this brief introduction, you’ll probably recall that in my last article of this series, I finished discussing how to implement some of the most useful web services provided by Yahoo! by using only a few basic PHP functions. In this way, you learned not only how to build simple mechanisms to call up these search services directly from your PHP 5 applications, but how to format the corresponding search results via a few basic (X)HTML tags.

However, all the practical examples that I gave you previously used a rather primitive procedural approach to implement the services in question within the context of different PHP 5 scripts. Logically, these examples can be really useful for learning the basic concepts surrounding the use of these search services, but undoubtedly, it’s necessary to modify and improve their source code to make it more compact and completely reusable.

Therefore, in this fourth chapter of the series, I’m going to show you how to use all of the Yahoo! Web Services discussed earlier, but this time by defining some custom PHP functions. In this way, it will complete the implementation of those services using a procedural approach.

Are you ready to learn how to use some custom PHP functions to take advantage of these helpful Yahoo! Web Search Services? Great! Let’s get going!

{mospagebreak title=A procedural approach to Yahoo! Search Service: defining a custom PHP function}

As I said in the beginning of this article, the native flexibility offered by PHP can be used in this case to define some straightforward custom functions to facilitate the implementation of a particular Yahoo! Web Service within a web application.

Having said that, I’m going to create a new PHP function which will use the already Yahoo Search Service that was profusely covered in the last article of this series. So pay close attention to the signature of this function, which I called “getWebSearchResults().” It looks like this:


// example using Yahoo! Web Search Service – results are displayed in a basic (X)HTML format utilizing an user-defined PHP function


// define ‘getWebSearchResults()’ function


function getWebSearchResults($query=’devshed.com’,$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://api.search.yahoo.com/WebSearchService/V1/webSearch?
appid=Your-AP-ID&query=’.$query.’&results=’.$numResults.’&output=php';

// 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].'</p><a
href="’.$result[Url].’">’.$result[Url].'</a>';

}

return $output;

}


You can see how the above “getWebSearchResults()” function does its thing. It takes up two input parameters, which is the search string that will be used to query the pertinent web search service along with the number of results that must be returned to client code.

Then this function performs a simple validation on these incoming arguments and fetches the corresponding search results, which were previously formatted using some basic (X)HTML tags before being sent back to the program’s flow. Of course, this previous formatting process is optional and the signature of the function can be easily modified to omit this part completely.

Well, now that I have explained how “getWebSearchResults()” works, please have a look at the following code sample, which shows how to use this function in a useful manner:


try{

// call ‘getWebSearchResults()’ function

echo getWebSearchResults();

}

catch(Exception $e){

echo $e->getMessage();

exit();

}


// displays the following results


Open Source Web Development Tutorials – Dev Shed


Offers a complete line of information on web page design tips, java scripting guide, shareware HTML editor, and web server software and setup.

http://www.devshed.com/

Webmaster Tools, Developer Tools, Programming Tools – Dev Mechanic


Do you know who your target audience is? If you don’t know, you are not … When faced with diminishing sales or a shift in profitability, sometimes a …

http://tools.devshed.com/

Web Hosting, Web Hosting Help, Web Hosting FAQs, Find Hosting – Web Hosting


Web Hosting, Web Hosting Help, Web Hosting FAQs, Find Hosting – Web Hosting … In this second part, I want to get more specific about the types of hosting …

http://webhosting.devshed.com/

PHP Help, PHP Programming, PHP Code, PHP Tutorials


A wide range of tutorials and articles with a growing PHP section. … Formerly referred to as "Personal Home Page Tools," PHP Hypertext Preprocessor …

http://www.devshed.com/c/b/PHP/

PHP Security Mistakes


Email Me Similar Content When Posted. Add Developer Shed Article Feed To Your Site. Email Article To Friend. Print Version Of Article. PDF Version Of Article …

http://www.devshed.com/c/a/PHP/PHP-Security-Mistakes/

Creating a Secure PHP Login Script


Email Me Similar Content When Posted. Add Developer Shed Article Feed To Your Site. Email Article To Friend. Print Version Of Article. PDF Version Of Article …

http://www.devshed.com/c/a/PHP/Creating-a-Secure-PHP-Login-Script/

MySQL Help, MySQL Tutorials, MySQL Programming, MySQL Tricks


A collection of MySQL tutorials on Administration, and Grant Tables. … MySQL is an open source relational database management system (RDBMS) …

http://www.devshed.com/c/b/MySQL/

Linux Web Hosting


Find your next Linux Web Host now! Search through our highly recommended Linux Web Hosting companies and read our Web Hosting FAQs and Web Hosting articles to help …

http://www.devshed.com/linux-web-hosting

Perl Help, Perl Programming, Perl Code, Perl Tutorials, Perl Development


A five-part beginning perl series that covers topics from regular expressions to file manipulation to database access for web developers.

http://www.devshed.com/c/b/Perl/

Dedicated Servers


Find your next Dedicated Servers Host now! Search through our highly recommended Dedicated Servers Hosting companies and read our Web Hosting FAQs and Web Hosting …

http://www.devshed.com/dedicated-servers


As you can see in the above example, the function does a decent job using the Yahoo! Web Search Service, since I passed the default term “Devshed.com” into it and it neatly returned some related results. Not to difficult to grasp, right?

So far, so good. At this point you can take a deep breath and relax, since you already learned how to create a custom PHP 5 function that implements the web search service provided by Yahoo! What else can you ask for?

Well, actually you can ask for much more when it comes to working with these web services and PHP 5. Thus, in the next section I’m going to define a similar PHP custom function. It will be responsible for using another popular Yahoo! service, namely the one that performs video searches.

To find out more about how this brand new PHP function will be built, please jump forward and read the next few lines. They’re just one click away.

{mospagebreak title=Using Yahoo! Video Search Service}

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?
appid=Your-AP-ID&query=’.$query.’&results=’.$numResults.’&output=php';

// 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].’
</p><p><img src="’.$result[Thumbnail][Url].’" width="’.$result
[Thumbnail][Width].’" height="’.$result[Thumbnail][Height].’" />
</p><a href="’.$result[Url].’">’.$result[Url].'</a>';

}

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….
here’s hoping 2005 is just as busy! Right Click And Save Target As
Madonna’s 2004 Movie Montage (WMV) – 7:44 – 21.8Mb
Noticias madonna – Noticias madonna – Noticias madonna -


http://madonnalicious.com/downloads/madonna_2004_montage.wmv

madonna_earlscourt_190804.wmv


http://www.madonnalicious.com/downloads/madonna_gelredome
_090904.wmv madonna 9sep 2004
http://www.madonnalicious.com/downloads/madonna_earlscourt
_190804.wmv
http://www.madonnalicious.com/downloads/madonna_earlscourt
_dtm_190804.wmv


http://www.madonnalicious.com/downloads/madonna_earlscourt_190804.wmv

madonna.al.et.mpg


Summary: Watch Madonna on Entertainment Tonight :
Madonna discusses American Life with ET .mpg Noticias madonna –
Noticias madonna – Noticias madonna -


http://steveswartz.com/madonna.al/madonna.al.et.mpg

madonna_jonathanross_ruperteverett.wmv


And Save Target As Friday Night With Jonathan Ross –
Rupert Everett On Madonna (WMV) – 1:25 – 2.45Mb JR
Noticias madonna – Noticias madonna – Noticias madonna -


http://www.madonnalicious.com/downloads/madonna
_jonathanross_ruperteverett.wmv

madonna_gos_240804.wmv


has just aired on local TV note the sound sync maybe
slightly off in the video file Right Click And Save Target As
Madonnas Visit To Great Ormond Street London Tonight
Report WMV 128 985Mb Noticias madonna –
Noticias madonna – Noticias madonna -


http://madonnalicious.com/downloads/madonna_gos_240804.wmv

madonna_earlscourt_dtm_190804.wmv


http://www.madonnalicious.com/downloads/madonna
_earlscourt_190804.wmv
http://www.madonnalicious.com/downloads/madonna
_earlscourt_dtm_190804.wmv
http://www.madonnalicious.com/downloads/madonna
_gelredome_music_090904.wmv


http://www.madonnalicious.com/downloads/madonna_
earlscourt_dtm_190804.wmv

The Androids: Do It With Madonna


Music Video by The Androids. Song: Do It With Madonna.
Label: Universal Records Released in 2003.


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 –
Featuring Madonna. Label: Zomba Recording Corp. Released in 2003.


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
baby boy from malawi, saying she has acted within the law.


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
of 13-month-old David claims he never intended his son to be
adopted by the pop diva, only for her to raise him.


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!

{mospagebreak title=Working with Yahoo! Image Search Service}

As you may have guessed, defining a custom PHP function that returns the results produced by the Yahoo! Image Search Service to client code is actually a process nearly identical to the one I discussed when working with video searches. Therefore, I’m not going to bore you with more explanations of how this brand new PHP function will work.

Instead, I’m going to show you its definition directly, in conjunction with a concrete example of how to use it.

Having clarified this point, here’s the corresponding signature of this custom PHP function:


// example using Yahoo! Image Search Service – results are displayed in a basic (X)HTML format utilizing an user-defined PHP function


// define ‘getImageSearchResults()’ function

function getImageSearchResults($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/ImageSearchService
/V1/imageSearch?appid=Your-AP-ID&query=’.$query.’
&results=’.$numResults.’&output=php';

// 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].’
</p><p><img src="’.$result[Thumbnail][Url].’" width="’.$result
[Thumbnail][Width].’" height="’.$result[Thumbnail][Height].’" />
</p><a href="’.$result[Url].’">’.$result[Url].'</a>';

}

return $output;

}


try{

// call ‘getImageSearchResults()’ function

echo getImageSearchResults();

}

catch(Exception $e){

echo $e->getMessage();

exit();

}


As I stated earlier, the above “getImageSearchResults()” is actually simple to grasp, since it implements practically the same business logic that you learned with the other Yahoo! web services. Anyway, to complete the explanation of this function, below I listed some real results returned by it when using the default “Madonna” search term:


// displays the following


Madonna_vogue.jpg


Madonna_3.jpg 30-Apr-2005 14:34 23k Madonna_9.jpg 30-Apr-2005
14:34 23k Madonna_vogue.jpg 30-Apr-2005 14:35 22k
Madonna_13.jpg 30-Apr-2005 14:34 22k


http://www.multinet.no/~jonarne/Hjemmesia/Favorittartister/
madonna/Madonna_vogue.jpg

madonna_90563a.jpg


ANGRER: Madonna spiller gjerne på sex for å provosere,
men angrer på boken Sex.- Jeg lurer stadig på hva som
motiverte meg til å lage den, sier hun om sin vågale bok, Sex ,
som vakte


http://pub.tv2.no/multimedia/TV2/archive/00090/madonna_90563a.jpg

madonna-079.jpg


madonna 079


http://www.celebritypicturesarchive.com/pictures/m/madonna/madonna-079.jpg

Joey Bday Madonna party017.jpg


Joey Bday Madonna party017


http://www.tock.org/~casalila/new/mainpics/Casa%20Lila/
Bday%20Parties/Joey’s%20Bday%20party%20Madonna%
20Party%204-98/Joey%20Bday%20Madonna%20party017.jpg

madonna-with-fish-red.jpg


madonna-with-fish-red.jpg


http://www.nndb.com/people/480/000023411/madonna-with-fish-red.jpg

30-madonna-britney.jpg


30 madonna britney


http://www.gmax.co.za/feel/08/pics/30-madonna-britney.jpg

madonna_out_cover.jpg


Could Madonna’s publicist explain why he gave the authorization
to publish such a bad photo for the cover of Out ! Anyway, Madonna
will take the stage next month at the seventh annual


http://www.ohlalaparis.com/photos/uncategorized/madonna_out_cover.jpg

madonna_116044a.jpg


BLIR SAKSØKT: Madonna kan ha latt seg inspirere for mye av Vogue-bilder.


http://pub.tv2.no/multimedia/TV2/archive/00116/madonna_116044a.jpg

madonna_reinvention6.jpg


Madonna is back!


http://schoolkrant.hsdrenthe.nl/archives/madonna_reinvention6.jpg

madonna16.jpg


Fotus da melhor cantora, mulher, mae, esposa, dançarina…
Enfim, a melhor humana!!! Lindaaaaaaaaaaaaaaa!!!
Don’t tell me TE AMOOOOOOOO!!!


http://livinha_minsane.zip.net/images/madonna16.jpg


Evidently Madonna generates passionate comments all over the world, but as you know, you should stay focused (at least for now) on studying and eventually improving the source code of all the custom PHP functions that you saw here, concerning the use of the most popular web services offered by Yahoo!

Final thoughts

In this fourth part of the series, I showed you how to create a few simple custom PHP functions for working with some of the most useful web search services provided by Yahoo! In this way, we build small pieces of code that can be reused any number of times.

In the next tutorial of the series, things will get even more exciting, since I’m going to teach you how to use the object-oriented paradigm to create a bunch of PHP 5 classes that will work in conjunction with all of the web services that were discussed earlier. You won’t want to miss it!

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye