The OData Producer Library for PHP is here

clip_image001

I’m pleased to announce that today we released the OData Producer Library for PHP. In case you missed it, we released last year a client library that allows PHP applications to consume an OData feed, and with this new library it now easy for PHP Applications to generate OData Feeds. PHP developers can now add OData support to their applications so it can be consumed by all clients and libraries that support OData.

The library is designed to be used with a wide range of data sources (from databases such as SQL Server and MySQL to data structures that are at the application level for applications such as CMS systems). The library is available for download under the open source BSD license: http://odataphpproducer.codeplex.com/
In order to make the library generic so it can be used on a wide range of scenarios we didn’t take any dependency to specific data structures or data sources. Instead the library is based on 3 main interfaces that, when implemented by the developers for the specific data source, allow the library to retrieve the appropriate data and serialize it for the client. The library takes care of handling metadata, query processing and serialization/deserialization of the data streams.

Two examples are included that show how a full OData service can be built using the library: the Northwind DB example uses an SQL Express DB as data source and the WordPress example that uses the WordPress’s MySQL DB Schema to expose a feed for Posts, Comments and Users.

Quick Introduction to OData

Open Data Protocol is an open protocol for sharing data. It is built upon AtomPub (RFC 5023) and JSON. OData is a REST (Representational State Transfer) protocol, therefore a simple web browser can view the data exposed through an OData service.

The basic idea behind OData is to use a well-known data format (Atom feed or JSON) to expose a list of entities.

The OData technology has two main parts:

  • The OData data model, which provides a generic way to organize and describe data. OData uses the Entity Data Model (EDM).The EDM models data as entities and associations among those entities. Thus OData work with pretty much any kind of data.
  • The OData protocol, which lets a client make requests to and get responses from an OData service. Data sent by an OData service can be represented on the wire today either in the XML-based format defined by Atom/AtomPub or in JavaScript Object Notation (JSON).

An OData client accesses data provided by an OData service using standard HTTP. The OData protocol largely follows the conventions defined by REST, which define how HTTP verbs are used. The most important of these verbs are:

  • GET : Reads data from one or more entities.
  • PUT : Updates an existing entity, replacing all of its properties.
  • MERGE : Updates an existing entity, but replaces only specified properties.
  • POST : Creates a new entity.
  • DELETE : Removes an entity.

Each HTTP request is sent to a specific URI, identifying some resource in the target OData service's data model.

The OData Producer Library for PHP

The OData Producer Library for PHP is a server library that allows to exposes data sources by using the OData Protocol.

The OData Producer supports all Read-Only operations specified in the Protocol version 2.0:

  • It provides two formats for representing resources, the XML-based Atom format and the JSON format.
  • Servers expose a metadata document that describes the structure of the service and its resources.
  • Clients can retrieve a feed, Entry or service document by issuing an HTTP GET request against its URI.
  • Servers support retrieval of individual properties within Entries.
  • It supports pagination, query validation and system query options like $format, $top, $linecount, $filter, $select, $expand, $orderby, $skip .
  • User can access the binary stream data (i.e. allows an OData server to give access to media content such as photos or documents in addition to all the structured data)

How to use the OData Producer Library for PHP

Data is mapped to the OData Producer through three interfaces into an application. From there the data is converted to the OData structure and sent to the client.

The 3 interfaces required are:

  • IDataServiceMetadataProvider: this is the interface used to map the data source structure to the Metadata format that is defined in the OData Protocol. Usually an OData service exposes a $metadata endpoint that can be used by the clients to figure out how the service exposes the data and what structures and data types they should expect.
  • IDataServiceQueryProvider: this is the interface used to map a client query to the data source. The library has the code to parse the incoming queries but in order to query the correct data from the data source the developer has to specify how the incoming OData queries are mapped to specific data in the data source.
  • IServiceProvider: this is the interface that deals with the service endpoint and allows defining features such as Page size for the OData Server paging feature, access rules to the service, OData protocol version(s) accepted and so on.
  • IDataServiceStreamProvider: This is an optional interface that can be used to enable streaming of content such as Images or other binary formats. The interface is called by the OData Service if the DataType defined in the metadata is EDM.Binary.

If you want to learn more about the OData Producer Library for PHP, the User Guide included with the code provides detailed information on how to install and configure the library, it also show how to implement the interfaces in order to build a fully functional OData service.

The library is built using only PHP and it runs on both Windows and Linux.

This is the first release of a Producer library, future versions may add Write support to be used for scenarios where the OData Service needs to provide the ability to update data. We will also keep it up to date with future versions of the OData Protocol.

Claudio Caldato, Principal Program Manager, Interoperability Strategy Team