SaaS in PHP: Google Map 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 IDE, PHP developers can consume popular web services such as Google Maps just by dragging-and-dropping these services from the IDE's Services window into PHP files.

This tutorial uses the Google Map service to demonstrate SaaS PHP support.

Contents

Content on this page applies to NetBeans IDE 7.0-7.3

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:

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 Google Map 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 GoogleMapApp. Save it in your default project folder, which is the web folder of your Apache server. Click Finish.
    Name and location page for GoogleMapApp project

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

Adding and Running the Google Map 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 Google Map 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 Google > Map Service > getGoogleMap.
    Services tab with expanded Google web services, showing Map Service
  3. In the editor, remove the existing <?php...?> tags from index.php. Drag the getGoogleMap 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.
  4. Leave all input parameters at default values and click OK. The IDE inserts the PHP for calling the Google Map service into the body of index.php, with the parameters you chose.
    <body>
    <?php
    include_once "org_netbeans_saas_google/GoogleMapService.php";
    try {
    $address = "16 Network Circle, Menlo Park";
    $zoom = 15;
    $iframe = "false"; $result = GoogleMapService::getGoogleMap($address, $zoom, $iframe);
    echo $result->getResponseBody();
    } catch (Exception $e) {
    echo "Exception occurred: " . $e;
    }
    ?>
    </body>
  5. Sign up for a Google map key at https://developers.google.com/maps/documentation/javascript/tutorial#api_key. The Google map key request dialog has a field for your website's URL. Type http://localhost in that field. Copy the key.
  6. In the Projects window, expand the org_netbeans_saas_google package and open googlemapservice.properties. Paste in the API key you got from Google
    Projects tab and editor in IDE showing where to paste key in GoogleMapServiceProfile.php
  7. In the org_netbeans_saas_google package, open GoogleMapService.php. Find the line $cl = new RestConnection(GEOCODE_URL, $queryParams); and change it to $cl = new RestConnection(GEOCODE_URL, $queryParams, null);. If you do not do this, you get a "Missing Argument 3 for RestConnection()" error when you run the project, even though the map displays correctly and the third parameter should just be null.
  8. Start your PHP engine.
  9. In the Projects window, right-click the project node and select Run (Shift-F6) from the context menu. A browser window opens, displaying the Google map of the address specified in index.php.
    Results in browser window

Send Feedback on This Tutorial

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 nbj2ee@netbeans.org mailing list.