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.
Apache 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:
Run the Xampp go-pear script (location varies by OS) to install PEAR. This creates a PEAR directory (again, location varies by OS).
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:
Optionally, add the path to PEAR to your Path environment variable (on Windows).
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:
Choose File > New Project. Under Categories, select PHP.
Under Projects, select PHP Application and click Next. The Name and Location page opens.
Name the project YahooNewsApp. Save it in your NetBeansProjects folder. Click Next. The Run Configuration page opens.
In the Run As: drop-down list, select Local Web Site (running on local web server). In the Project URL folder, type http://localhost/news. Select "Copy Files from Sources Folder to another location". Next to the Copy to Folder field, click Browse. The Select Folder Location dialog opens.
Browse to the parent folder from which php files are exposed on localhost. For Xampp, this is XAMPP_HOME/xampp/htdocsCreate a new subfolder named news. Click Open, and the dialog closes. The path to the news folder is now in the Copy to Folder field of the Run Configuration page. Click Finish.
The IDE creates the project and copies the source folders to the directory from which they will be exposed by the web server. The project's index.php file opens in the editor. Open a file explorer and check that index.php has been copied to the correct directory.
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:
In the IDE, open the project's index.php file in the editor and open the Services window.
In the Services window, expand the Web Services node and navigate to Yahoo > News Search Service > [newsSearch > Search.
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.
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.
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/>";
}
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.
Start your PHP engine.
In the Projects window, right-click index.php 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.
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.