Query SAP Netweaver Gateway Data Using the OData SDK for PHP



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

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

Set Up the API Server

Follow the steps below to begin producing secure SAP Netweaver Gateway 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 SAP Netweaver Gateway

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

SAP Gateway allows both basic and OAuth 2.0 authentication. You can use basic authentication to connect to your own account, or you can use OAuth to enable other users to retrieve data from your service with their accounts. In addition to authenticating, set the following connection properties to access SAP Gateway tables.

  • Url: Set this to the URL of your environment, or to the full URL of the service. For example, the full URL might appear as: https://sapes5.sapdevcenter.com/sap/opu/odata/IWBEP/GWSAMPLE_BASIC/. In this example, the environment URL would just be: https://sapes5.sapdevcenter.com.
  • Namespace: Set the appropriate Service Namespace. In the example above, IWBEP is the namespace. It is optional if the full URL to the service is specified.
  • Service: Set this to the service you want to retrieve data from. In the example above, the service is GWSAMPLE_BASIC. It is not required if the full URL is specified.

Authenticate via Basic Authentication

In basic authentication, you use your login credentials to connect. Set the following properties:

  • User: This is the username you use to log in to SAP Gateway.
  • Password: This is the password you use to log in to SAP Gateway.

Authenticate via OAuth Authentication

You can connect to SAP Gateway using the embedded OAuth connectivity (without setting any additional authentication connection properties). When you connect, the OAuth endpoint opens in your browser. Log in and grant permissions to complete the OAuth process. See the OAuth section in the online Help documentation for more information on other OAuth authentication flows.

You can then choose the SAP Netweaver Gateway 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\SAPGateway\
  • Unix or Mac OS X: ~/cdata/SAPGateway/

Work with SAP Netweaver Gateway 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\SAPGatewayEntities.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 SAP Netweaver Gateway data using an object-oriented interface in PHP. The code below creates a record of the SalesOrderLineItems table and then retrieves the live data, showing the newly created record.

    require_once 'SAPGatewayEntities.php'; try{ $svc = new CData(); $salesorderlineitems = new SalesOrderLineItems(); $salesorderlineitems->Quantity = '15'; $svc->AddToSalesOrderLineItems($salesorderlineitems); $svc->SetSaveChangesOptions(SaveChangesOptions::None); $svc->SaveChanges(); $response = $svc->salesorderlineitems()->Execute(); foreach($response->Result as $salesorderlineitems) echo $salesorderlineitems->ProductID."
    "; } catch (Exception $e) { //catch errors from the API Server echo $e->getError(), "\n"; }

Ready to get started?

Learn more or sign up for a free trial:

CData API Server