Ready to get started?

Learn more:

DB2 Connectivity Solutions

Query DB2 Data Using the OData SDK for PHP



You can use the API Server and the OData PHP SDK to easily consume DB2 entities in PHP by modeling them as objects.

The CData API Server, when paired with the ADO.NET Provider for DB2, exposes DB2 data (or data from any of 200+ other ADO.NET Providers) as Web services. You can follow the procedure below to work with DB2 data as PHP objects.

Set Up the API Server

Follow the steps below to begin producing secure DB2 OData services:

Deploy

The API Server runs on your own server. On Windows, you can deploy using the stand-alone server or IIS. On a Java servlet container, drop in the API Server WAR file. See the help documentation for more information and how-tos.

The API Server is also easy to deploy on Microsoft Azure, Amazon EC2, and Heroku.

Connect to DB2

After you deploy the API Server and the ADO.NET Provider for DB2, provide authentication values and other connection properties needed to connect to DB2 by clicking Settings -> Connections and adding a new connection in the API Server administration console.

Set the following properties to connect to DB2:

  • Server: Set this to the name of the server running DB2.
  • Port: Set this to the port the DB2 server is listening on.
  • Database: Set this to the name of the DB2 database.
  • User: Set this to the username of a user allowed to access the database.
  • Password: Set this to the password of a user allowed to access the database.

You will also need to install the corresponding DB2 driver:

  • Windows: Install the IBM Data Server Provider for .NET.

    On Windows, installing the IBM Data Server Provider is sufficient, as the installation registers it in the machine.config.

  • Java: Install the IBM Data Server Driver for JDBC.

    In the Java version, place the IBM Data Server Driver JAR in the www\WEB-INF\lib\ folder for this application.

You can then choose the DB2 entities you want to allow the API Server access to by clicking Settings -> Resources.

Additionally, configure the API Server for compatibility with the OData SDK for PHP: Click Server -> Settings and in the OData section set Default Version to 2.0.

Authorize API Server Users

After determining the OData services you want to produce, authorize users by clicking Settings -> Users. The API Server uses authtoken-based authentication and supports the major authentication schemes. You can authenticate as well as encrypt connections with SSL. Access can also be restricted based on IP address; by default, only connections from the local machine are allowed.

For simplicity, we will authenticate to the API Server by setting the authtoken in the URL. This is not enabled by default; you will need to add the following lines to the API Server configuration file, settings.cfg.

[Application] AllowAuthTokenInUrl = true

The settings.cfg file is located in the data directory. In the .NET edition, the data directory is the app_data folder under the www folder. In the Java edition, the data directory's location depends on the operation system:

  • Windows: C:\ProgramData\CData\DB2\
  • Unix or Mac OS X: ~/cdata/DB2/

Work with DB2 Entities as PHP Objects

Follow the steps below to use the ODataPHP SDK to create a proxy class that will connect to the Web services exposed by the API Server.

  1. Pass the URL to a command like the one below:

    php C:\PHPLib\ODataphp\PHPDataSvcUtil.php /uri=https://your-server:8032/api.rsc/@your-authtoken/ /out=C:\PHPLib\ODataphp\DB2Entities.php

    The preceding command defines classes from the metadata returned in the response from the OData endpoint, then outputs the class definitions to the specified folder.

    Both the API Server and the OData SDK for PHP support forms and Windows authentication. The API Server uses authtokens to authenticate users authorized to access the OData endpoint. You can supply authtokens in HTTP Basic authentication or append them to the OData URL.

    You can configure authorized users in the Settings -> Users section of the API Server administration console.

  2. You can now start accessing DB2 data using an object-oriented interface in PHP. The code below creates a record of the Orders table and then retrieves the live data, showing the newly created record.

    require_once 'DB2Entities.php'; try{ $svc = new CData(); $orders = new Orders(); $orders->ShipCity = 'New York'; $svc->AddToOrders($orders); $svc->SetSaveChangesOptions(SaveChangesOptions::None); $svc->SaveChanges(); $response = $svc->orders()->Execute(); foreach($response->Result as $orders) echo $orders->OrderName."
    "; } catch (Exception $e) { //catch errors from the API Server echo $e->getError(), "\n"; }