Home arrow PHP arrow Page 2 - Defining Some Custom PHP Functions with Yahoo Web Services

A procedural approach to Yahoo! Search Service: defining a custom PHP function - PHP

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.

TABLE OF CONTENTS:
  1. Defining Some Custom PHP Functions with Yahoo Web Services
  2. A procedural approach to Yahoo! Search Service: defining a custom PHP function
  3. Using Yahoo! Video Search Service
  4. Working with Yahoo! Image Search Service
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
February 05, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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