Web Services and PHP – SOAP vs XML-RPC vs REST

What is web services?

In a typical web surfing scenario, a visitor visits a website and use the functionality provided by that particular website.HTTP request is send to server from web browsers and server responses are translated by browser to display the desired result of the visitor. But, this scenario has been changed in the recent days. You don’t need to visit the particular website to use their service and functionality if they are providing web services. Web services are set of platform independent exposed APIs(functions) which can be used used from remote server over the Internet. There are basically two parties involved in this, one which provides a set of exposed APIs and the another one ,commonly know as web services consumers,is the party which uses the functionality and services provided by web services providing party.

There are different method for providing web services but the most common are SOAP, XML-RPC and REST .

SOAP

SOAP was the acronym of Simple Object Access Protocal but this acronym was dropped in the version of 1.2 of SOAP. It is method for exchanging XML based message over the Internet for providing and consuming web services. SOAP message are transferred forming the SOAP-Envelope.You can view the typical SOAP Message articture from here. SOAP is widely criticized for it’s design complexity.

In PHP 5, there is built-in extension for the providing and consuming web services. But, I personally prefer Nusoap toolkit of PHP for providing and consuming web services using SOAP in PHP.

XML-RPC

XML-RPC (remote procedure call) another way of providing and consuming web services. It uses XML to encode and decode the remote procedure call along with it’s parameter. Compared to the articture of SOAP, it has simpler architecture. You can even define data type of parameters of procedure in XML-RPC. You can visit the official website www.xmlrpc.com to know more about XML-RPC.

In PHP, there is extension which contain various functions for facilating XML-RPC request and response. But the functions xmlrpc_encode_request() and xmlrpc_decode_request() available in PHP is very useful for when it comes to encode and decode XML-RPC request and response.

I’ve built  Nepali Currency Converter using XML-RPC web services provided by foxrate.org.

REST

Representational State Trasfer(REST) is comparatively simpler method for providing and consuming web services. Nowadays, this method is becoming popular in the arena of web services. Unlike above two method, it is not necessary to use XML as a data interchange format in REST. REST architecture is basically focused on two things : Resources and Interface.RESTful  is another term to define REST web services .

Resources are application’s state and functionality which is represented by a unique URL. The resources share a uniform interface to transfer the state between the client and server.

For example the URL, http://example.com/product/11 can be a resource.Suppose, GET method is used to retrieve product detail from that URL, POST method is used to modify the production information and DELETE method can be used to delete the product from the same URL. Here, the HTTP methods works as a interface to access the resources.

Talking about PHP, the format of information(representation) returned can be in XML, JSON or even in HTML format. DOM functions, SimpleXML functions and JSON functions comes handy when you are handling RESTful interfaces in PHP.

I also want to know your view. Please participate in the poll below.

Which style of web service is easy to use and consume in your view ?

View Results

Loading ... Loading ...

17 thoughts on “Web Services and PHP – SOAP vs XML-RPC vs REST

  1. Enrique Serra

    Nice articule. I need protect a web service made in php. How i do that?

    Thanks!

  2. @Enrique – Web services are made for using by other what do you want to protect in it? For security you’ve to use the security mechanism in PHP…any token or API Key is the better idea for this..

  3. Enrique Serra

    The web service checks against a data base if there is new updates for a software. The client who consume the web servie is developed in vb.net. My question is how can i protect the comunication beetwen the client and the server. I don’t know how to do this.

    Can you help me whit some web page…?

    Thanks a lot!

  4. @Enrique – web services are made for consuming and I don’t understand why you want to stop the communication of client with the server. If you want to do so then you can disable the web service provided by you by stopping taking the request or deleting that page etc….

  5. I have used SOAP.. so I voted for it.. but REST seems to be topping the chart.. :)

  6. Rowland Watkins

    @Enrique, the simplest approach that can be used with SOAP, XML-RPC, and REST is Transport Layer Security (TLS). This means rather than using http://, you can use https:// – all traffic is encrypted.

  7. Enrique Serra

    I need stop the service if the client who consumes the web service is not valid.
    I want implement ws-security in the web service whit soap. Is possible? Can i send the credentials to the data base encrypted?
    Thanks!

  8. DC

    I enjoyed reading your article. I’ve worked with both SOAP and REST methods. I write REST APIs when you just have to retrieve something. But if some complex task is required like validating and then inserting data in database, I go for SOAP.

  9. venkatadapa

    your article is very useful and clearly understandable, Thank you so much friend

  10. I am still using nusoap library.

  11. sandman

    are there any examples for producing rest webservices in php

  12. REST is latest. So I prefer this.

  13. Pete

    Thanks for the clear explanation – I can’t believe it’s written by an Nepali, even though the grammar’s a bit odd.

  14. nice post thank you

  15. Mythri

    Hi Roshan,

    Thanks a lot! i found most of the solutions in this blog.

    Now i am creating service center for a mobile group. two interrelated drop down i got the code (country and state). Can you please please tell me how to display all the branches when i select the the country?

    Please help me in this….

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>