corner imagecorner image
FeaturesPluginsPlatformDocs & SupportCommunityPartners

SaaS in PHP: Yahoo News Service Tutorial

NetBeans IDE supports Software as a Service (SaaS) applications in PHP. SaaS refers to a software application delivery model where a software vendor develops a web-native software application and hosts and operates the application for use by its customers over the Internet. SaaS is an increasingly popular model for providing software functionality as it is economical in terms of both cost and customer hardware resources. By using SaaS PHP support in Netbeans 6.5, PHP developers can consume popular web services such as Google Maps, Yahoo News etc, just by dragging-and-dropping these services from the IDE's Services window into PHP files.

This tutorial uses the Yahoo News Search service to demonstrate SaaS PHP support.

Contents

Content on this page applies to NetBeans IDE 6.5

To follow this tutorial, you need the following software and resources.

Software or Resource Version Required
NetBeans IDE PHP download bundle. Must include the SaaS Services
Code Generation plugin (PHP category)
A PHP engine including PEAR
(PHP web service packages)
Version 5. Can be standalone components or an AMP package.
See Getting Started with PHP Development in NetBeans.
A web serverApache HTTP Server is suggested.
The web server is included in AMP packages.

Installing and Setting Up PEAR Packages

In order to use SaaS services in PHP you must have the PHP Extension and Application Repository (PEAR) installed. After PEAR is installed, include the path to PEAR in the include_path variable in your php.ini file. Then install the following PEAR packages:

  • Http_Request
  • Net_URL
  • Net_Socket

The procedure for installing and setting up PEAR varies according to the operating system you are using and the AMP package you have installed, if any. For example, to install PEAR in the Xampp package:

  1. Run the Xampp go-pear script (location varies by OS) to install PEAR. This creates a PEAR directory (again, location varies by OS).
  2. Open php.ini and add the path to the newly-created PEAR directory to the include_path variable for your operating system. Uncomment include_path if it is commented out. For example, using Xampp on MacOS, the include_path variable is:
    include_path=".:/Applications/xampp/xamppfiles/lib/php:/Applications/xampp/xamppfiles/lib/php/pear"

    Using Xamp on Windows, with Xampp installed to the root of G:/, the include_path variable is:

    ; Windows: "\path1;\path2"
      include_path = ".;G:\xampp\php\pear\"
  3. Optionally, add the path to PEAR to your Path environment variable (on Windows).
  4. Open a command prompt in the PEAR directory and run the command pear install Http_Request. This installs the Http_Request package and any packages on which Http_Request has dependencies, which include Net_URL and Net_Socket. If these packages are already installed, PEAR returns "Nothing to install."

Creating the PHP Project

After you set up PHP and PEAR, create the PHP project that will use the Yahoo News Service.

To create the project:

  1. Choose File > New Project. Under Categories, select PHP. Under Projects, select PHP Application and click Next. The Name and Location page opens.
  2. Name the project YahooNewsApp. Save it in your default project folder, which is the web folder of your Apache server. Click Finish.
    New PHP project wizard showing name and location page

The IDE creates the project as a Local Web Project, run on your local Apache server at http://localhost/YahooNewsApp/. The project's index.php file opens in the editor.

Adding and Running the Yahoo News Web Service

You add SaaS operations to PHP files by dragging the operation from the Web Service Manager in the Services window and dropping it into the body of your PHP file.

To add the Yahoo News Service to your PHP project:

  1. In the IDE, open the Services window.
  2. In the Services window, expand the Web Services node and navigate to Yahoo > News Search Service > [newsSearch] > Search.
    Services tab with expanded Yahoo web services, showing News Search 'search' operation
  3. In the editor, remove the existing <?php...?> tags from index.php. Drag the search operation from the Services window and drop it into the <body> element of index.php. A dialog opens for setting initial values of input parameters.
    Input Parameters dialog opening as a result of dragging and dropping Yahoo News Service search operation into index.php
  4. You can leave all input parameters at default values. The "query" parameter refers to the subject of news stories you are searching for and you can set it to any arbitrary value. In the example, it is set to "php", so the service will search for stories about PHP. You can set it to "NetBeans", "football," etc. Click OK when you are done. The IDE inserts the PHP for calling the Yahoo News Search service into the body of index.php, with the parameters you chose.
    <body>

    <?php

    include_once "org_netbeans_saas_yahoo/YahooNewsSearchService.php";
    try {
    $query = "php";
    $type = "all";
    $results = 10;
    $start = 1;
    $sort = "rank";
    $language = null;
    $output = "xml";
    $callback = null;

    $result = YahooNewsSearchService::search($query, $type, $results, $start, $sort, $language, $output, $callback);
    echo $result->getResponseBody();
    } catch(Exception $e) {
    echo "Exception occured: ".$e;
    }
    ?>
    </body>
  5. Customize the PHP so that it displays the output in an attractive and readable form. Otherwise when you run the file, the browser will display all the news items in an endless wrapped line. In the following example, the line echo $result->getResponseBody(); is commented out and new output code is added.
    //Comment this line
    //echo $result->getResponseBody();
    
    //Add the following lines to display the news nicely.
    $xml = $result->getDataAsXml();
    echo "<h2>Yahoo news search results for ".$query."</h2>";
    foreach ($xml->Result as $item) {
        echo "<a href=\"".$item->Url."\" target=\"_blank\">".$item->Title."</a><br/>";
        echo $item->Summary."<br/><br/>";
        }
  6. Configure the API key. Yahoo News Search does not require an API key, but you still have to pass a non-null value. In the Projects window, expand the org_netbeans_saas_yahoo package and open YahooNewsSearchAuthenticatorProfile.php in the editor. Type in any non-null value for the $api_key variable.
    Projects tab and editor in IDE showing an arbitrary value being assigned to $api_key in YahooNewsSearchAuthenticatorProfile.php
  7. Start your PHP engine.
  8. In the Projects window, right-click the project node and select Run (Shift-F6) from the context menu. A browser window opens, displaying all news results that match the value of your "query" parameter.
    Results in browser window of Yahoo News Service search for PHP


See Also

For more information about using NetBeans IDE to develop PHP projects, RESTful web services, SaaS, and other Java EE applications, see the following resources:

To send comments and suggestions, get support, and keep informed on the latest developments on the NetBeans IDE Java EE development features, join the mailing list.