PHP
  Home arrow PHP arrow Page 2 - Defining Some Custom PHP Functions with Yahoo Web Services
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Defining Some Custom PHP Functions with Yahoo Web Services
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    2008-02-05


    Table of Contents:
  • Defining Some Custom PHP Functions with Yahoo Web Services
  • A procedural approach to Yahoo! Search Service: defining a custom PHP function
  • Using Yahoo! Video Search Service
  • Working with Yahoo! Image Search Service

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    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

     
     
    ADVERTISEMENT


    Defining Some Custom PHP Functions with Yahoo Web Services - A procedural approach to Yahoo! Search Service: defining a custom PHP function
    ( Page 2 of 4 )

    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
     

       

    PHP ARTICLES

    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek