September 18th, 2008Do REST in PHP – PHP RESTful Data Services
In RESTful paradigm we give a piece of data ( or in other word ‘Resource’) a unique URL. And in order to manipulate data we use HTTP verbs POST/PUT (create, update), GET (read), DELETE (delete). For an example
take the scenario of manipulating Students data in a high school. Here is how each operation is mapped to a http request (URL + HTTP verb)
| HTTP request | Operation |
| POST api/students/ben | Create the resource (peice of data) called ben as a student. HTTP body or the url itself (e.g. api/students/ben?age=15&country=xx) may contain the required information about ben |
| GET api/students/ben | Retrieve the information about ben. |
| PUT api/students/ben | Update ben |
| DELETE api/student/ben | Delete the student called ‘ben’. |
With the addition of all these HTTP verbs WSO2 WSF/PHP 2.0.0 become a great tool for RESTful developers. Specially with the introducing Data Services library it was so easy to make your database a REST service. I m thinking of preparing a series of application to demonstrate the power of WSF/PHP with all these new features.
This demo -RESTful School- shows how you map a URL to a peice of data. Here we use only the http “GET” method (which is the most to used in practicle data service).
Here is some description of the operations you find in there. Just check the source code for RESTful School demo to see how this is done in code level.
| Operation | URL | SQL Query | Note |
| Get All subjects |
subjects
|
SELECT subjectName, subjectTeacher FROM Subjects
|
With no parameters |
| Get subject information From Name |
subjects/{name}
|
SELECT subjectName, subjectTeacher FROM Subjects where SubjectName = ?
|
The single parameter feed from prepared statement syntax |
| Get All students |
students
|
SELECT * FROM Students
|
Again no parameters |
| Get students From Name |
students/{name}
|
Inner Query:
SELECT subjectName, marks FROM Marks m, Subjects s ". " where m.studentId = ? and m.subjectID = s.subjectId Outer Query SELECT * FROM Students where StudentName = ?
|
Nested query, Inner query is called from outer query |
| Get Marks per Students per Subjects |
students/{student}/marks/{subject} |
SELECT marks FROM Marks, Subjects, Students where StudentName = ?". " and SubjectName = ? and Marks.subjectId = Subjects.subjectId". " and Marks.studentID = Students.StudentId; |
Two parameters, and ‘?’ in the sql query.. |
![[Ask]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/ask.png)
![[Bloglines]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/bloglines.png)
![[del.icio.us]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/digg.png)
![[diigo]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/diigo.png)
![[dzone]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/dzone.png)
![[Facebook]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/facebook.png)
![[Google]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/google.png)
![[MySpace]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/myspace.png)
![[MyWeb]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/myweb.png)
![[Newsvine]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/newsvine.png)
![[PlugIM]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/plugim.png)
![[Reddit]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/reddit.png)
![[Slashdot]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/slashdot.png)
![[Spurl]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/spurl.png)
![[StumbleUpon]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/stumbleupon.png)
![[Twitter]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/twitter.png)
![[Windows Live]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/windowslive.png)
![[Yahoo!]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/yahoo.png)
![[Email]](http://www.dimuthu.org/wp-content/plugins/bookmarkify/email.png)
September 20th, 2008 at 2:34 pm
[...] Do REST in PHP – PHP RESTful Data Services | Dimuthu's Blog (tags: php rest restful) [...]
September 27th, 2008 at 9:35 am
[...] wrote a simmlar blog on Data Services last week to demonsrates how you design the mapping of url to different [...]
September 30th, 2008 at 3:36 am
[...] School demo with PHP REST Framework The entry by Dimuthu explains clearly how to implement a REST application with PHP using PHP REST Framework, ground up, adhering to the best practices of [...]
October 7th, 2008 at 12:49 pm
[...] Do REST in PHP – PHP RESTful Data Services [...]
October 10th, 2008 at 2:23 pm
[...] single script. I have written about how you can expose your Database as a REST and SOAP services in few of my previous posts using the Data Service capability of WSF/PHP. But there can be situations where your service is not [...]
January 2nd, 2009 at 4:32 am
If you like to read more about REST with PHP, read this book RESTful PHP Web Services.
My review of this book can be found here, RESTful PHP Web Services – Book Review.