Last week I wrote a how to on Writing a SOAP and REST Service with PHP. It shows how to write a single script that enables the SOAP and REST web services interfaces using WSF/PHP. Today I’m going to show you how you can use WSF/PHP to write clients in both SOAP and REST form.
You can see in the library code It is using different values for “useSOAP” argument depending on what form of messaging we like to use. Amazon SOAP service uses the SOAP 1.1 and the REST service uses the “GET” HTTP method for messaging. We can configure these details as mentioned in the following table.
Since Amazon requires different request messages for SOAP and REST services, we have to create them separately depending on the request type. But the response returns by the service is same in both cases so we can handle it using the same code.
Here is how ItemLookup operation of the Amazon service is written within the above mentioned constrains.
When you are developing Web Service for CRUD (Create, Read, Update, Delete) operations you may find it is easy to implement it as RESTful service. In this Demo on RESTful CRUD Service You can have an idea how you develop such a service with WSO2 WSF/PHP.
Here we take a scenario of submitting applications (say for a school).
In RESTful world we map a resource to a unique URL. In this demo, application is a resource. We use the URL “application/{id}” to represent a particular application with the id {id}.
You can use HTTP verb + Resource URL touples to manipulate the resource with CRUD operations. Here is how it is done in this particular demonstration.
Request format (HTTP Verb + URL)
Operation Semantic
POST applications/{id}
Create an application
GET applications/{id}
Get an application
PUT applications/{id}
Change an application
DELETE applications/{id}
Delete an application
Go for the wsf/php demo sitefor the live demo of this service. Visit the demo service source code to see how easy to implement it with WSF/PHP Data Services library.
I wrote a similar blog on Data Services last week to demonstrate how you design the mapping of url to different resources in a RESTful Service.
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).
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;