PHP
  Home arrow PHP arrow Page 2 - Using Yahoo! Web Services with PHP 5
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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? 
PHP

Using Yahoo! Web Services with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2008-01-08

    Table of Contents:
  • Using Yahoo! Web Services with PHP 5
  • Getting started using the Yahoo! Web Search services
  • Implementing the Yahoo! Video Search Service with PHP 5
  • Querying the Yahoo! Image Search Service with PHP 5

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

    Dell PowerEdge Servers

    Using Yahoo! Web Services with PHP 5 - Getting started using the Yahoo! Web Search services
    (Page 2 of 4 )

    Before you start using a specific Yahoo! Web Service within your own PHP 5 scripts, first you should get an “Application ID,” which is a key provided by Yahoo! that will let you use the services in question in the scope of your web site’s domain. As you might guess, to get this application ID you must complete a simple registration form, and if all goes well, you should receive the key in your email inbox in a very short time. Nothing too difficult, actually.

    Once you’ve received this key by email, then you’re ready to use the web services provided by Yahoo!, so considering that you’ve already completed this process successfully, it’s time to develop some code samples that will help you to grasp more clearly how to implement a specific web service with PHP 5.

    All right, to begin with, a simple GET request must be triggered to the URL where a particular web service runs, in order to get its corresponding results. These results, unless you specify another format, will be returned as XML data.

    To demonstrate how to make a request to the Yahoo! Web Search Service, below I coded an illustrative PHP 5 script, which first queries the mentioned web service using “Madonna” as the search term, and then fetches the corresponding results in XML format.

    Now that I have explained how this sample script will work, please study its respective signature:


    try{

    // example using Yahoo! Search Web Service - search results are
    displayed in raw XML format

    $request='http://search.yahooapis.com/WebSearchService/V1/
    webSearch?appid=Your-AP-ID&query=madonna&results=4';

    // trigger the http request

    if(!$results=file_get_contents($request)){

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

    }

    // display the results in XML format

    header('Content-type:text/xml;charset=iso-8859-1');

    echo $results;

    }

    catch(Exception $e){

    echo $e->getMessage();

    exit();

    }


    As you can see, using the Yahoo! Web Search Service with PHP 5 is actually a no-brainer process that only requires a few lines of code. In this case, the above code snippet triggers the request to the proper URL, including into the query string the application ID that you got previously, then the pertinent search term and finally (this parameter is optional) the number of results that must be returned to the browser.

    Lastly, these results are fetched by using the “file_get_contents()” PHP built-in function (make sure that “http” wrappers are enabled on your php.ini file), and displayed directly on the browser, in the following way:


    <ResultSet xsi:schemaLocation="urn:yahoo:srch
    http://api.search.yahoo.com/WebSearchService/V1/
    WebSearchResponse.xsd" type="web"
    totalResultsAvailable="65700000" totalResultsReturned="4"
    firstResultPosition="1"
    moreSearch="/WebSearchService/V1/webSearch?query=madonna&appid=
    Your-AP-ID&region=us">

    <Result>

    <Title>Madonna</Title>

    <Summary>Official site of pop diva Madonna, with news, music, media, and fan club.</Summary>

    <Url>http://www.madonna.com/</Url>

    <ClickUrl>http://uk.wrs.yahoo.com/
    _ylt=A0Je5aqLHBZHhCMBkjbdmMwF;
    _ylu=X3oDMTB2cXVjNTM5BGNvbG8DdwRsA1dTMQRwb3MDMQRzZWMDc3IEdnRpZAM-
    /SIG=11buvpo71/EXP=1192717835/**http%
    3A//www.madonna.com/</ClickUrl>

    <DisplayUrl>www.madonna.com/</DisplayUrl>

    <ModificationDate>1192518000</ModificationDate>

    <MimeType>text/html</MimeType>

    <Cache>

    <Url>http: //uk.wrs.yahoo.com/
    _ylt=A0Je5aqLHBZHhCMBkzbdmMwF;
    _ylu=X3oDMTBwOHA5a2tvBGNvbG8DdwRwb3MDMQRzZWMDc3IEdnRpZAM-
    /SIG=15ipfur0l/EXP=1192717835/**http%
    3A//66.218.69.11/search/cache%3Fei=UTF-8%26appid= Your-AP-ID %
    26query=madonna%26results=4%26u=www.madonna.com/%26w=madonna%
    26d=ZjRUGudmPoUK%26icp=1%26.intl=us

    </Url>

    <Size>18519</Size>

    </Cache>

    </Result>

    <Result>

    <Title>MySpace.com - Madonna - Pop / Rock -
    www.myspace.com/madonna</Title>

    <Summary>Madonna MySpace page with news, blog, music downloads,
    desktops, wallpapers, and more.</Summary>

    <Url>http://www.myspace.com/madonna</Url>

    <ClickUrl>http://uk.wrs.yahoo.com/_ylt=A0Je5aqLHBZHhCMBlTbdmMwF;
    _ylu=X3oDMTB2ZjQ4dDExBGNvbG8DdwRsA1dTMQRwb3MDMgRzZWMDc3IEdnRpZAM-
    /SIG=11i0d3vg8/EXP=1192717835/**http%3A//www.myspace.com/madonna

    </ClickUrl>

    <DisplayUrl>www.myspace.com/madonna</DisplayUrl>

    <ModificationDate>1192518000</ModificationDate>

    <MimeType>text/html</MimeType>

    <Cache> <Url>http://uk.wrs.yahoo.com/_ylt=A0Je5aqLHBZHhCMBljbdmMwF;
    _ylu=X3oDMTBwZG5hOWwzBGNvbG8DdwRwb3MDMgRzZWMDc3IEdnRpZAM-
    /SIG=15pd5lje6/EXP=1192717835/**http%3A//66.218.69.11/search/cache%
    3Fei=UTF-8%26appid= Your-AP-ID %26query=madonna%26results=4%
    26u=www.myspace.com/madonna%26w=madonna%26d=Z-awV-dmPogu%
    26icp=1%26.intl=us

    </Url>

    <Size>138157</Size>

    </Cache>

    </Result>

    <Result>

    <Title>Madonna (I)</Title>

    <Summary>Facts and filmography for the singer and actress.</Summary>

    <Url>http://www.imdb.com/name/nm0000187</Url>

    <ClickUrl>http://uk.wrs.yahoo.com/
    _ylt=A0Je5aqLHBZHhCMBmDbdmMwF;
    _ylu=X3oDMTB2M3MzdmZoBGNvbG8DdwRsA1dTMQRwb3MDMwRzZWMDc3IEdnRpZAM-
    /SIG=11m0tssno/EXP=1192717835/**http%
    3A//www.imdb.com/name/nm0000187

    </ClickUrl>

    <DisplayUrl>www.imdb.com/name/nm0000187</DisplayUrl>

    <ModificationDate>1192518000</ModificationDate>

    <MimeType>text/html</MimeType>

    <Cache>

    <Url>http://uk.wrs.yahoo.com/
    _ylt=A0Je5aqLHBZHhCMBmTbdmMwF;
    _ylu=X3oDMTBwZ2dpZ2ZkBGNvbG8DdwRwb3MDMwRzZWMDc3IEdnRpZAM-
    /SIG=15tn2vem4/EXP=1192717835/**http%
    3A//66.218.69.11/search/cache%3Fei=UTF-8%26appid= Your-AP-ID %
    26query=madonna%26results=4%26u=www.imdb.com/name/nm0000187%
    26w=madonna%26d=DgNm1OdmPoYh%26icp=1%26.intl=us

    </Url>

    <Size>97655</Size>

    </Cache>

    </Result>

    <Result>

    <Title>Madonna Song Lyrics: Home Page</Title>

    <Summary>Extensive collection of Madonna lyrics, organized by album. Includes lyrics to singles.</Summary>

    <Url>http://www.madonnalyrics.org/</Url>

    <ClickUrl>http://uk.wrs.yahoo.com/
    _ylt=A0Je5aqLHBZHhCMBmzbdmMwF;
    _ylu=X3oDMTB2OWM1MXQ5BGNvbG8DdwRsA1dTMQRwb3MDNARzZWMDc3IEdnRpZAM-
    /SIG=11h8ms67a/EXP=1192717835/**http%3A//www.madonnalyrics.org/

    </ClickUrl>

    <DisplayUrl>www.madonnalyrics.org/</DisplayUrl>

    <ModificationDate>1181286000</ModificationDate>

    <MimeType>text/html</MimeType>

    <Cache>

    <Url>http://uk.wrs.yahoo.com/
    _ylt=A0Je5aqLHBZHhCMBnDbdmMwF;
    _ylu=X3oDMTBwYzFnNmkxBGNvbG8DdwRwb3MDNARzZWMDc3IEdnRpZAM-
    /SIG=15o801re7/EXP=1192717835/**http%
    3A//66.218.69.11/search/cache%3Fei=UTF-8%26appid= Your-AP-ID %
    26query=madonna%26results=4%26u=www.madonnalyrics.org/%
    26w=madonna%26d=WcyQU-dmPohM%26icp=1%26.intl=us

    </Url>

    <Size>18749</Size>

    </Cache>

    </Result>

    </ResultSet>

    <!--

    ws02.search.scd.yahoo.com uncompressed/chunked Wed Oct 17
    07:30:35 PDT 2007

    -->


    Undoubtedly, at first glance the output returned by the Yahoo! Web Search service seems to be rather intimidating, but this is a mistaken impression. If you take a closer look at it, you’ll see that the entire output is wrapped into a pair of <ResultSet></ResultSet> nodes, and each of the pertinent results are contained in a pair of <Result></Result> tags too.

    This obviously implies that it’s perfectly possible to build a simple XML parser with PHP 5 that displays these results in a more readable format, but this process is out of the scope of this article. In addition, if you’re interested in learning more on this topic, you may want to look at the bunch of examples created by Rasmus Lerdorf, which are available for educational purposes, on the aforementioned Yahoo! Developer Network’s official web site.

    So far, so good. At this point I’m pretty sure that you grasped the logic that stands behind querying the Yahoo! Web Search Service with PHP 5. This is merely the beginning, however, since in the next section I’m going to show you how to implement the Video Search Service provided by Yahoo!, by using a script similar to the one coded previously.

    To see how this brand new Yahoo! web service can be utilized with PHP 5, please go ahead and read the next few lines. I’ll be there, waiting for you.

    More PHP Articles
    More By Alejandro Gervasio


       · The implementation of different web services with PHP 5 can be sometimes, a...
     

       

    PHP ARTICLES

    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery
    - Using Timers to Benchmark PHP Applications
    - Benchmarking Applications with PHP
    - Setting Up a Web-Based File Manager: PHPfile...
    - Developing a Modular Class For a PHP File Up...
    - Setting Up a Web-Based File Manager: bfExplo...
    - Defining a Custom Function for File Uploader...
    - Parsing Child Nodes with the DOM XML extensi...
    - Creating an Error Handling Module for a PHP ...
    - Accessing Attributes and Cloning Nodes with ...
    - Retrieving Information on Selected Files wit...
    - Handling HTML Strings and Files with the DOM...
    - Building File Uploaders with PHP 5

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway