The OData PHP Producer library is a server side implementation of OData Protocol using PHP.
I was involved in the design and implementation of this library, thought of sharing some high level details of this project.
The "OData PHP Producer" is designed in a generic way such that you can expose data from any data source ( relational database, file system etc..)
The above diagram shows the high-level design of "OData PHP Producer" library and a set of sample services (NorthWind, WordPress). These sample services can be used as a reference to write your own service. Each sample service has the following components:
I was involved in the design and implementation of this library, thought of sharing some high level details of this project.
The "OData PHP Producer" is designed in a generic way such that you can expose data from any data source ( relational database, file system etc..)
The above diagram shows the high-level design of "OData PHP Producer" library and a set of sample services (NorthWind, WordPress). These sample services can be used as a reference to write your own service. Each sample service has the following components:
· IServiceProvider and IDataService implementation, the main service entry point.
· IDataServiceMetadataProvider implementation.
· IDataServiceQueryProvider implementation.
· IDataServiceStreamProivder implementation [optional].
The flexibility of exposing data from any data source requires the end developer, the one want to expose the data as OData service, to implement the above set of interfaces defined by the "OData PHP Producer" library.
1. IDataServiceMetadataProvider:
Purpose of implementation of this interface is to let the "OData PHP Producer" library know the metadata of your data source, then the library will exposes a $metadata endpoint which can be used by the clients to fetch the service metadata document that describes how the service exposes the data and what structures and data types they should expect
2. IDataServiceQueryProvider:
The library don't know anything about the underlying data source such as how to connect, how to query etc.. It’s the responsibility of the end developer to connect to the data source, fetch the data and give it to library based on the delegates invoked by the library.
The OData protocol defines URI conventions to address the resources in your data source and to control the amount and order of the data that an OData service returns for the resource identified by the URI. The library has the code to parse the incoming OData queries, these queries are translated into call to IDataServiceQueryPorivder methods
The OData protocol defines URI conventions to address the resources in your data source and to control the amount and order of the data that an OData service returns for the resource identified by the URI. The library has the code to parse the incoming OData queries, these queries are translated into call to IDataServiceQueryPorivder methods
Note that there are two versions of Query Provider interface, IDataServiceQueryProvider and IDataServiceQueryProvider2. The IDataServiceQueryProvider2 is same as IDataServiceQueryProvider except one difference that with IDataServiceQueryProvider library does the filtering using built-in expression provider and if result-set is huge then performance of the library will not up to the marks.
The IDataServiceQueryProvider2,library generates the filter criteria with the help of custom expression provider and the IDataServiceQueryProvider2 can use this filter criteria to run query on the data source. So in this case library don’t need to scrub out the unnecessary records to get the final subset of data.
3. IDataStreamProvider:
This is an optional interface that can be used to enable streaming of content such as Images or other binary formats
4. IServiceProvider and IDataService:
IDataService 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. IServiceProvider is the interface that deals with access to the above interface implementations from the library.
================
Before going into the details of implementing the above interfaces for your data source let us configure the "OData PHP Producer" library and associated sample service based on MySQL WordPress database.
================
Before going into the details of implementing the above interfaces for your data source let us configure the "OData PHP Producer" library and associated sample service based on MySQL WordPress database.
Configuring the library and running MySQL based OData service on Windows
Prerequistes
1. PHP version > 5.3.x
Download and install PHP.
Refer this link to configure PHP on IIS.
2. IIS UrlRewrite Module
Download and install IIS URL Rewrite module from here
3. MySQL
4. The sample service showcasing how to expose a MySQL database as OData Service uses WordPress database. Download and install WordPress from here
Download and install PHP.
Refer this link to configure PHP on IIS.
2. IIS UrlRewrite Module
Download and install IIS URL Rewrite module from here
3. MySQL
4. The sample service showcasing how to expose a MySQL database as OData Service uses WordPress database. Download and install WordPress from here
Installation
1. Download the latest "OData PHP Producer" from official github site https://github.com/MSOpenTech/odataphpprod
2. Extract the zip file to your local drive, for example E:\
include_path = ".;E:\OData Producer for PHP\library"
5. Open the file
[root]\OData Producer for PHP\services\IDataServiceQueryProvider2 Implementation\WordPress\ wordpressQueryProvider.php
Update the value of connection parameters DB_NAME, DB_USER, DB_PASSWORD and DB_HOST so that it points to your WordPress MySQL database
6. Restart IIS by running iisreset command from command prompt
7. Your service is ready :), open your browser and go to the website we created in step4, append the service name 'WordPress2.svc' to the root URL.
e.g: http://localhost:8083/WordPress2.svc
This will cause the "OData PHP Producer library"to return the Service Document of WordPress data service.
You can refer official OData site for more about URI options supported by an OData service.
Here few service URI that you can try:
* http://localhost:8083/WordPress2.svc/$metadata
Fetches the metadata of WordPress database.
* http://localhost:8083/WordPress2.svc/Posts
Fetches post entity instances from the database
* http://localhost:8083/WordPress2.svc/Posts?$expand=Category
Fetches the post entity instances and category instance associated with each post.
Hi,
ReplyDeleteThanks for your information.
We had created an Odata service for our site,But we have some issues by accessing the service , actually we don't want to display some fields in the service (like password).
But that field is require for order by, filtering etc. can you pls help us to avoid some fields from the service ( but we need the field in meta data and also for filtering )
Hi Geo,
ReplyDeleteExposing a property in "entity metadata" and hiding the same property in "entity instance" is not supported in OData PHP framework.
Thanks
Anu
Hi Anu, thanks for your tutorial. But I wonder where I could change the service Wordpress2.svc name?
ReplyDeleteIn web development PHP is no doubt highly used and preferred programming. But we have to admit that PHP 5 unleashed itself with its advanced features, people understood its power. Take a look: php
ReplyDelete