Query NetSuite Data Using the OData SDK for PHP



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

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

About NetSuite Data Integration

CData provides the easiest way to access and integrate live data from Oracle NetSuite. Customers use CData connectivity to:

  • Access all editions of NetSuite, including Standard, CRM, and OneWorld.
  • Connect with all versions of the SuiteTalk API (SOAP-based) and SuiteQL, which functions like SQL, enabling easier data querying and manipulation.
  • Access predefined and custom reports through support for Saved Searches.
  • Securely authenticate with Token-based and OAuth 2.0, ensuring compatibility and security for all use cases.
  • Use SQL stored procedures to perform functional actions like uploading or downloading files, attaching or detaching records or relationships, retrieving roles, getting extra table or column info, getting job results, and more.

Customers use CData solutions to access live NetSuite data from their preferred analytics tools, Power BI and Excel. They also use CData's solutions to integrate their NetSuite data into comprehensive databases and data warehouse using CData Sync directly or leveraging CData's compatibility with other applications like Azure Data Factory. CData also helps Oracle NetSuite customers easily write apps that can pull data from and push data to NetSuite, allowing organizations to integrate data from other sources with NetSuite.

For more information about our Oracle NetSuite solutions, read our blog: Drivers in Focus Part 2: Replicating and Consolidating ... NetSuite Accounting Data.


Getting Started


Set Up the API Server

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

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

The User and Password properties, under the Authentication section, must be set to valid NetSuite user credentials. In addition, the AccountId must be set to the ID of a company account that can be used by the specified User. The RoleId can be optionally specified to log in the user with limited permissions.

See the "Getting Started" chapter of the help documentation for more information on connecting to NetSuite.

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

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

    require_once 'NetSuiteEntities.php'; try{ $svc = new CData(); $salesorder = new SalesOrder(); $salesorder->Class_Name = 'Furniture : Office'; $svc->AddToSalesOrder($salesorder); $svc->SetSaveChangesOptions(SaveChangesOptions::None); $svc->SaveChanges(); $response = $svc->salesorder()->Execute(); foreach($response->Result as $salesorder) echo $salesorder->CustomerName."
    "; } 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